5

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).
Suge
  • 2,808
  • 3
  • 48
  • 79
  • possible duplicate of [NSFileManager creating folder (Cocoa error 513.)](http://stackoverflow.com/questions/3439408/nsfilemanager-creating-folder-cocoa-error-513) – Alex Reynolds Jul 23 '13 at 07:25
  • See: http://stackoverflow.com/a/3439986/19410 for a previous answer to this question – Alex Reynolds Jul 23 '13 at 07:25
  • @AlexReynolds, I don't think that question/answer should be considered a duplicate, in this case. This question is about a jailbreak app, installed in `/Applications/`. Such an app will have a different documents directory, and have to use different APIs to create it. – Nate Jul 23 '13 at 07:34

2 Answers2

3

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

cahn
  • 1,350
  • 1
  • 12
  • 24
  • Thank you, but where is the right place for the apps like mine to write files with permissions natively? – Suge Jul 23 '13 at 02:05
  • I don't know what is norm for jailbroken apps. For appstore apps `[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]` is the place. – cahn Jul 23 '13 at 02:42
  • @Bob, as I say in my answer, the recommended location would be `/var/mobile/Library/YOURAPPNAME`. – Nate Jul 23 '13 at 04:14
2

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • @ Nate, I met another problem, because I should hook UIResponder of every apps include SpringBoard and any others.In the hooking, there will write something to the specified file.If I set the path to /var/mobile/Library/MyApp, I found that only the SpringBoard and MyApp could write successfully. So is there a place every app can write and read? – Suge Jul 23 '13 at 10:16
  • @Bob, are you hoping to hook **normal** apps (App Store apps) that are installed under `/var/mobile/Applications/`? If so, then I don't think there's any location outside their sandbox that you use for this. I'll double check for you, and post more later. Possibly, there's some location like the Photos folder, or Music folder, that you could use ... I'm not sure. – Nate Jul 23 '13 at 20:40