System() command not working when enabling app sandboxing

痞子三分冷 提交于 2019-12-11 14:53:08

问题


Apple now require all future apps to be sandboxed and so I followed the instructions to sandbox an app. The build succeeded but then my system(rm -rf ~/.Trash/*) command stopped working. Nothing happened. What I find confusing here is why this system command does not work with App Sandboxing/Entitlements on. Here is are my entitlement settings:

Entitlements: Checked

App Sandboxing: Checked

And here is my current code:

- (void)viewDidLoad {
[self emptyTrash];
}

- (void)emptyTrash {
system(rm -rf ~/.Trash/*);
}

Thanks for your help!


回答1:


Take a look at documentation.

Mac OS X path-finding APIs, above the POSIX layer, return paths relative to the container instead of relative to the user’s home directory. If your app, before you sandbox it, accesses locations in the user’s actual home directory (~) and you are using Cocoa or Core Foundation APIs, then, after you enable sandboxing, your path-finding code automatically uses your app’s container instead.

you can use

struct passwd *getpwuid(uid_t uid);
struct passwd {
char    *pw_name;       /* user name */
char    *pw_passwd;     /* encrypted password */
uid_t   pw_uid;         /* user uid */
gid_t   pw_gid;         /* user gid */
__darwin_time_t pw_change;      /* password change time */
char    *pw_class;      /* user access class */
char    *pw_gecos;      /* Honeywell login info */
char    *pw_dir;        /* home directory */
char    *pw_shell;      /* default shell */
__darwin_time_t pw_expire;      /* account expiration */
}

 #include <pwd.h>
 #include <sys/types.h>
 char *HomeDirectory = getpwuid(getuid())->pw_dir;
 NSLog(@"%s", HomeDirectory);
 system([[NSString stringWithFormat:@"rm -rf %s/.Trash/",HomeDirectory] UTF8String]);


来源:https://stackoverflow.com/questions/11291164/system-command-not-working-when-enabling-app-sandboxing

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