I have an app where I save data to a file before quitting (or going to background) and that restores it reading from the file when restarting the app. The problem is that this works perfectly on the simulator, but doesn’t on a real device.
Here is the code I use to get the path (on applicationDidFinishLaunching):
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:@"archive"];
NSLog(@"Reading from filePath:%@",path);
if ([[NSFileManager defaultManager]fileExistsAtPath:path])
{
//Init data from file
}
else
{
//Present the view where the users has to enter info for the first time (this is what always gets executed)
}
And this is the code that I use to save the data(on applicationDidEnterBackground):
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:@"archive"];
NSLog(@"Reading from filePath:%@",path);
NSMutableData *data=[[NSMutableData alloc]init];
//Put info to save in data
[data writeToFile:path atomically:YES];
I’ve checked in the iphone’s console, and when loading the info It print’s a directory that looks normal(I don’t have the device here so I can’t post the exact directory) , and then I see a deny file-write-create error like this:
unknown sandboxd[1332] <Notice>: AppName(1328) deny file-write-create
When I have acces to the device again (tomorrow) I’ll update the post with the exact directory printed. Any Idea why this happens?
Try using
stringByAppendingPathComponentinstead ofstringByAppendingString