问题
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