【问题标题】:Strange problem with reading and writing a plist file读写plist文件的奇怪问题
【发布时间】:2011-05-31 21:42:56
【问题描述】:

我有一个从我的 plist 文件中读取信息的应用程序。为此,我使用以下代码:

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

为了编写它,我使用以下代码:

- (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 Store 上提交了应用程序,但其他用户无法读取/写入此文件。 为什么这个? 谢谢 保罗

【问题讨论】:

  • 是苹果拒绝了它,还是他们接受了它但用户有问题?你知道错误信息是什么意思吗?
  • 为什么不简单地使用 NSArray 或 NSDictionary 来读写属性列表呢?这就像容易十倍。 WTP?
  • 为什么不使用 NSUserDefaults?它用于设置,本质上是一个 plist。
  • @WTP 你能发布同样的例子吗?谢谢
  • 您确定调用 saveSetting 方法时没有收到任何错误吗?该代码根本不应该工作。

标签: iphone objective-c plist writing


【解决方案1】:

您不能写回应用程序包。在写入之前,您必须将原始 plist 文件复制到文档目录或任何其他可写位置。

一个例子

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

【讨论】:

  • 谢谢迪帕克,我该怎么做?所以问题是文件 settings.plist 存在于其他用户的应用程序目录中,但他们可以写,对吧?如何修复权限?谢谢
  • 尝试从库目录中读取文件,如果您发现该文件不存在,请将主文件(应用程序附带的 settings.plist)复制到库目录中。现在开始使用该文件进行读写。
  • 感谢 deepack,库目录在哪里?应用启动时如何在库中复制此文件?
  • 添加了一个例子。您可以像application:didFinishLaunchingWithOptions: 那样在应用程序启动时执行此操作,也可以仅在需要时才检查和复制它。
  • @deepak: 你能帮我做点什么吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2012-03-09
  • 1970-01-01
相关资源
最近更新 更多