-->

Strange problem with reading and writing a plist f

2019-04-13 18:22发布

问题:

I have an application that read info from I plist file. To do it I use this code below:

  NSData *plistData;  
    NSString *error;  
    NSPropertyListFormat format;  
    id plist;  
    localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];  
    plistData = [NSData dataWithContentsOfFile:localizedPath];   

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];  
    if (!plist) {  
        NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);  
        [error release];  
    }  




    NSString *tel=[NSString stringWithFormat:@"tel:%@",[plist objectForKey:@"number"]];
    NSURL *telephoneURL = [NSURL URLWithString:tel];
    [[UIApplication sharedApplication] openURL:telephoneURL];

And to write it I use this code:

- (IBAction) saveSetting:(id)sender{



    NSData *plistData;  
    NSString *error;  
    NSPropertyListFormat format;  
    id plist;  

    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];  
    plistData = [NSData dataWithContentsOfFile:localizedPath];   

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&error];  
    if (!plist) {  
        NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);  
        [error release];  
    }  

    NSLog([plist objectForKey:@"message"]);
    [plist setValue:textMex.text forKey:@"message"];
    NSLog([plist objectForKey:@"message"]);

    NSLog([plist objectForKey:@"number"]);
    [plist setValue:textNumero.text forKey:@"number"];
    NSLog([plist objectForKey:@"number"]);

    [plist setValue:@"NO" forKey:@"firstTime"];

    [plist writeToFile:localizedPath atomically:YES];

    [self aggiorna];




    [settingScreen removeFromSuperview];

}

Now I have a big problem, tha app work properly in all my developer device and in the simulator and the app read and write properly the file. I submit the app on the Apple store but others user can't read/write this file. Why this? Thanks Paolo

回答1:

You can't write back to the application bundle. You will have to copy the original plist file to the documents directory or any other writable location before it can be written to.

An example

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString libraryPath = [paths objectAtIndex:0];
NSString plistPath = [libraryPath stringByAppendingPathComponent:@"settings.plist"];

// Checks if the file exists at the writable location.
if ( ![[NSFileManager defaultManager] fileExistsAtPath:plistPath] ) {
    NSString *masterFilePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];

    // Try to copy the master file to the writable location
    NSError *error;
    if ( ![[NSFileManager defaultManager] copyItemAtPath:masterFilePath toPath:plistPath error:&error] ) {
        NSLog(@"Error: %@", [error localizedDescription]); 
        // Serious error.
    }
}

...
// Ready for use.
NSData *plistData = [NSData dataWithContentsOfFile:plistPath];