Mac OS X:从目录服务获取当前用户的当前用户名和主目录 [英] Mac OS X: Get current username and home directory for current user from Directory Services

查看:579
本文介绍了Mac OS X:从目录服务获取当前用户的当前用户名和主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是用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:从目录服务获取当前用户的当前用户名和主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆