开发者

How to get macosx Log-in User Name in objective-c

开发者 https://www.devze.com 2022-12-31 00:59 出处:网络
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. ThanksThere\'s NSUserName()C function:getpwuid() which return the

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消