I want to write a file in the Documents folder of my iPhone app.
But when I try to open the path I get the
Cocoa error 257 – Permission Denied.
Here’s a little code snippet:
// create path to file
NSURL *path = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
NSLog(@"%@", [path description]);
NSError *error = nil;
NSString *filename = [NSString stringWithContentsOfURL:path encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", error);
NSLog(@"%@", [filename description]);
Can you tell me please, what’s wrong with that code?
Thanks!
It looks as though you’re trying to open the documents directory rather than a file in that directory.
stringWithContentsOfURLloads the contents of the file into theNSString, so it’s not a filename.If
filenameshould be a path rather than the contents of a file, usestringWithContentsOfURL:instead.