我的应用程序是用Objective-C编写的.如何通过目录服务获取当前登录用户的用户名和主目录?
My app is written in Objective-C. How can I get the user name and home directory of the currently logged in user through directory services?
详细信息: 我的Cocoa应用使用getenv("USER")和getenv("HOME")获取当前的用户名和主目录.显然,如果用户通过目录服务登录,这将无法正常工作.我正在执行chdir($ HOME),然后运行此命令: 登录-fp $ USER 并且它拒绝了它,因为我认为登录通过目录服务来对用户进行身份验证,并且该用户在该系统中的用户名可能与$ USER的值不同.同样,他可能在目录服务中指定的主目录与$ HOME不同.
Detail: My Cocoa app gets the current username and home directory with 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. Likewise, he may have a home directory specified in directory services that is different than $HOME.
推荐答案尝试NSUserName()和NSHomeDirectory().
更多推荐
Mac OS X:从目录服务获取当前用户的当前用户名和主目录
发布评论