Strange problem with reading and writing a plist file

穿精又带淫゛_ 提交于 2019-12-09 20:16:36

问题


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];


来源:https://stackoverflow.com/questions/6193912/strange-problem-with-reading-and-writing-a-plist-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!