How to get macosx Log-in User Name in objective-c
please let me know how to get the currently logged in user's name or mac machine name (if possib开发者_Python百科le) in objective-c. Thanks
There's NSUserName()
C function: getpwuid() which return the struct passwd:
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
----------------------Code----------------
#include <pwd.h>
register uid_t uid;
struct passwd *uid_pw;
uid = geteuid ();
uid_pw = getpwuid (uid);
strcpy(uname,uid_pw->pw_name)
//similarly other information
You can use NSUserName()
. This will return the name of the current logged in user.
NSString *userName = NSUserName();
精彩评论