Nili
Nili

Reputation: 2003

Cocoa: iOS: fopen fails sometimes with error: "Operation not permitted"

I have a very weird behaviour in which I sometimes get a "Operation not permitted" error when using fopen in my code, and sometimes everything runs smoothly.

My first thought was a sandbox issue, but in both cases I write the files to the same location. I run only up to 4 fopens before I fclose them, so I don't believe I maxed-out my files descriptors. The next batch of files will always run after the 4 previous ones were fclosed.

I'm out of ideas how to approach this ambiguous behaviour, and would love to hear what you have in mind.

Thanks,

Nili

Upvotes: 1

Views: 2592

Answers (1)

Eyal
Eyal

Reputation: 124

May be related to data protection caused by Passcode set on the Device's Settings. If the fopen is called when the device was locked and passcode is needed, "Operation not permitted" will be returned.

Use these events in AppDelegate to identify this scenario and stop/start file manipulation:

- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application;
- (void)applicationProtectedDataDidBecomeAvailable:(UIApplication *)application;

See also: UIApplicationDelegate documentation

Upvotes: 3

Related Questions