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

折月煮酒 提交于 2019-12-21 19:53:47

问题


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?

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.


回答1:


Try NSUserName() and NSHomeDirectory().



来源:https://stackoverflow.com/questions/4262426/mac-os-x-get-current-username-and-home-directory-for-current-user-from-director

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!