WriteToFile runs on the simulator, but not on the device

I have a plist that I am changing:

NSString *finalPath = [path stringByAppendingPathComponent:@"KeyFrameFileByMovie.plist"];
NSMutableDictionary *keyFrameFileByMovie = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
[keyFrameFileByMovie setValue:keyFrameName forKey:movieName];
BOOL isOk = [keyFrameFileByMovie writeToFile:finalPath atomically:YES];

On the simulator, isOk is 1 on the device isOK is 0

I do not think this is a random problem, because I have a receipt code:

NSString *finalPath = [path stringByAppendingPathComponent:@"KeyFrameFileByMovie.plist"];<br>
NSDictionary *plistData =[[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

Why does the writeToFile function not work on the device?

+3
source share
4 answers

The iPhone application has a very strict directory structure. Unfortunately, device permissions against the simulator vary. Thus, the only problem here is that you are not saving in the Documents directory, but in the MainBundle directory.

In the above example, what is the meaning of the path?

+6
source

? , iPhone , Mac () , . finalPath , .

+4

Add "/" by adding the line

NSString *finalPath = [path stringByAppendingPathComponent:@"/KeyFrameFileByMovie.plist"];
+3
source
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];
NSString *stringFilepath = [docPath stringByAppendingPathComponent:@"configlocaljson.json"];[replacedString writeToFile:stringFilepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSError * error = nil;
BOOL success = [replacedString writeToFile:stringFilepath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"Success = %d, error = %@", success, error);

His work is for me.

0
source

All Articles