Question

I got a real strange problem with an iOS app I'm currently working at. The effect only exists if I test the app using ad hoc distribution. After updating the app (it has to be installed before) it wasn't working correctly. I could track the error down and it is caused by following line of code:

[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:&error];

Now you could say, of course: Don't ever write to the app bundle itself, but the base path is the Documents folder via:

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
imgDir = [docsDir stringByAppendingPathComponent:@"images"];
folder = [imgDir stringByAppendingPathComponent:md5]; // md5-Hash is created before

The complete error message (logged to iphone system log) is:

Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x14d44f30 {NSFilePath=/var/mobile/Applications/280C6D36-3667-4589-A74F-42F3F17ABA71/Documents/images/39b6cd45a05a2276ef065b2ecf33b1eb, NSUnderlyingError=0x14d4e340 "The operation couldn’t be completed. Operation not permitted"}

The interesting thing is, as I noted, if I delete the app before installing via ad hoc distribution (Testflight to a iPhone 5 with iOS 7.0.4) the folder is created and the App works as expected. The only references I could find were developers not using stringByAppendingPathComponent or writing directly to the app bundle. Maybe anybody else got the problem or has an idea?

Was it helpful?

Solution

I finally found the reason why the folder could not be created. Afterwards it seems pretty simple and stupid, but if you could take a look at the complete source code you would unterstand how this could happen. In my defense I have to say that I came to this project for further development because the original developer left the project. For your better understanding I simplified the code a lot.

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
imgDir = [docsDir stringByAppendingPathComponent:@"images"];

The imgDir was actually saved in userPreferences and a proxy class always returned the saved path, which is a really bad idea. During the update process the app gets a new guid which reflects in the apps documents path. So trying to create a folder in the previous version documents folder had to fail as it doesn't exist anymore. I corrected the code to never save the path and always return the current one with the code above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top