Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "Cocoa error 513" when write files to "/var/mydir/files" on an jailbroken iPhone?

My app will write some files to file system and it is installed in /Application not in /var/mobile/Application, On my develop iPhone, every things goes right.But when distribute to others, They got "Cocoa error 513".The files are written at /var/mydir/files, What's that problem? Where is right place for me to write with full permission? Thank you.

Code:

NSString *dir = @"/var/mydir/docs/";
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dir]) {
    NSError *error = nil;
    BOOL createdDir = [fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&error];
    if (!createdDir) {
        UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Attention" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlertView show];
    }
}

Error:

The operation couldn't be completed. (Cocoa error 513).
like image 895
Suge Avatar asked Jul 23 '13 00:07

Suge


2 Answers

Cocoa error 513 is NSFileWriteNoPermissionError. You can find it in Foundation Constants Reference. Maybe you don't have write permission for /var.

like image 119
cahn Avatar answered Sep 21 '22 20:09

cahn


As @cahn said, your app is being denied because of a permission problem. Even if installed in /Applications/, apps normally run as user mobile. As you can see from this login session:

$ ssh mobile@iphone5
mobile@iphone5's password: 
iPhone5:~ mobile$ cd /var
iPhone5:/var mobile$ mkdir mydir
mkdir: cannot create directory `mydir': Permission denied

the mobile user doesn't have write permissions directly under /var/.

If you take a look at this document on building Cydia apps on thebigboss.org, you'll see that they recommend that jailbreak apps create documents directories at /var/mobile/Library/YOURAPPNAME:

1) Appstore app runs in a sandbox at /var/mobile/Applications/GUID/folder. Jailbroken app runs in /Applications

2) Appstore app has a Documents folder that is created by the installation process. Jailbroken app does not. It is up to the app to create its own folder. Should you need this type of folder, you must create this with a simple mkdir command in your applicationDidFinishLaunching function. Just add a simple function: mkdir(“/var/mobile/Library/YOURAPPNAME”, 0755); If the folder already exists, no harm done. You want to do this because the install process runs as user root and the app runs as user mobile. If Cydia does this for you then the folder will have the incorrect permissions.

like image 40
Nate Avatar answered Sep 22 '22 20:09

Nate