The operation couldn’t be completed. Operation not permitted

20,259

You are not allowed to write to the application bundle, I'm surprised it works on any of your devices. There are various places you can write, depending on what your purpose is:

  • If you want to store the file until you delete it, write to the documents directory: [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  • If you want to allow the system to delete it if the device is running low on space (and don't care if it is saved when the device is backed up), use the caches directory: [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  • If you're just saving it temporarily while you process it and will delete it right away, use the temporary directory: NSTemporaryDirectory()

Also, BTW, it might be cleaner to use [directory stringByAppendingPathComponent:filename] rather than [NSString stringWithFormat:@"%@/%@", directory, filename] to construct paths.

Share:
20,259
Teddy
Author by

Teddy

Updated on April 09, 2020

Comments

  • Teddy
    Teddy about 4 years

    Very strange problem with my iPhone App. We have an App that has been approved and is in sales at the App Store. It contains a feature to download some database updates. The update comes in a ZIP via HTTP. The problem is that I cannot save this downloaded ZIP because I get the "The operation couldn’t be completed. Operation not permitted" error.

    BUT: this happens on 2 phones out of 10. If a phone cannot save the file it cannot do at all. If I redownload the app from the store it does not change it. But those phones who are capable to save the ZIP are always capable. All phones run the same iOS version and all of them are iPhone 4. This really drives me crazy.

    If I start XCode one phone gives no error in debug the other gives. And they always give.

    Here is the code:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [activeResponse release];
    [theConnection release];
    
    NSLog(@"%d", [receivedData length]);
    NSString *s = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
    NSLog(@"%@", s);
    [s release];
    [theRequest release];   
    NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"temp.zip"];
    NSLog(path);
    NSError * error;
    if ([receivedData writeToFile:path options:NSDataWritingAtomic error:&error])
        NSLog(@"Success");
    else 
        NSLog(@"Error");
    if (error)
        NSLog([error description]);
    

    Any ideas, please?

  • Teddy
    Teddy about 13 years
    Thanks for your reply (both part). 8 out of 10 phones allow you to do so. All examples available online uses the bound path to save NSData. Seems they should not. But then can you give me some hints please? If I deliver my app with a database (XML file) and I want to update it how to do it?
  • Anomie
    Anomie about 13 years
    @Teddy: Use NSFileManager's fileExistsAtPath: to test if the file exists in the documents directory. If not, you could either copy the version from your bundle or just read it directly from the bundle.
  • Teddy
    Teddy about 13 years
    Cool. Thanks. I have to solve the problem if I release a new db version with my bundle. But it is easy to do. Thx for your response.