读取和写入plist文件的奇怪问题 [英] Strange problem with reading and writing a plist file

查看:121
本文介绍了读取和写入plist文件的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从I plist文件中读取信息的应用程序。要做到这一点,我使用下面的代码:

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

}

现在我遇到了一个大问题,即应用程序正常运行我的所有开发人员设备以及模拟器和应用程序都可以正确读写文件。
我在Apple商店提交应用程序,但其他用户无法读取/写入此文件。
为什么这样?
谢谢
Paolo

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

推荐答案

您无法回写应用程序包。您必须先将原始plist文件复制到文档目录或任何其他可写位置,然后才能写入。

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.

示例

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

这篇关于读取和写入plist文件的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆