简体   繁体   English

Mac OS X:从目录服务获取当前用户的当前用户名和主目录

[英]Mac OS X: Get current username and home directory for current user from Directory Services

My app is written in Objective-C. 我的应用程序是用Objective-C编写的。 How can I get the user name and home directory of the currently logged in user through directory services? 如何通过目录服务获取当前登录用户的用户名和主目录?

Detail: My Cocoa app gets the current username and home directory with getenv("USER"), getenv("HOME"). 细节:我的Cocoa应用程序使用getenv(“USER”),getenv(“HOME”)获取当前用户名和主目录。 Apparently this doesn't work right if the user is logged in through directory services. 显然,如果用户通过目录服务登录,则此功能无法正常工作。 I am doing a chdir($HOME) and then running this command: login -fp $USER and it rejects it because, I think, login goes through the directory service to authenticate the user, and his username in that system may not be the same as the value of $USER. 我正在做一个chdir($ HOME)然后运行这个命令:login -fp $ USER并拒绝它,因为我认为登录通过目录服务来验证用户,并且他在该系统中的用户名可能不是与$ USER的值相同。 Likewise, he may have a home directory specified in directory services that is different than $HOME. 同样,他可能在目录服务中指定的主目录与$ HOME不同。

尝试NSUserName()NSHomeDirectory()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM