无法在真实设备上更新plist文件

时间:2015-08-28 23:26:09

标签: ios objective-c iphone xcode plist

在我的应用程序中,我想从我的网站下载men.plist文件,这是Pickerview的数据存储,并替换该men.plist文件的以前的本地版本。它可以工作,如果我在iPhone模拟器上测试它,如果我在真实设备上运行它不工作。

以下是我在ViewController.m中的代码:

NSString *urlPath = [NSString stringWithFormat:@"http://somedomain.com/men.plist"];

NSURL *url = [NSURL URLWithString:urlPath];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *responceData, NSError *connectionError) {

    if (responceData) {

        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"men" ofType:@"plist"];

        NSDictionary *replacement = [[NSDictionary alloc] initWithContentsOfURL:url];

        [[replacement description] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

之后在我的ClothDataStorage.m课程中,我做了:

- (instancetype)init
{
self = [super init];
if (self) {

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"men" ofType:@"plist"];
    NSDictionary *clothDataInfo = [NSDictionary dictionaryWithContentsOfFile: plistPath];

    _countryInfoStorage = [[CountryInfoStorage alloc] initWithClothDataInfo:clothDataInfo];

    _clothInfoStorage = [[ClothInfoStorage alloc] initWithClothDataInfo:clothDataInfo];

    _sizeInfoStorage = [[SizeInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
}
return self;

}

我比较了来自ViewController的filePath和来自Cloth DataStorage.m的plistPath,它在两种情况下都相同,用于模拟器:

  

(lldb)po filePath   / Users / someuser / Library / Developer / CoreSimulator / Devices / 97AC5070-9286-479A-9C31-78974BA982F4 / data / Containers / Bundle / Application / 05C8CB5B-9869-4D44-847B-203A237255E5 / Size Chart.app/men.plist < / p>      

(lldb)po plistPath   / Users / someuser / Library / Developer / CoreSimulator / Devices / 97AC5070-9286-479A-9C31-78974BA982F4 / data / Containers / Bundle / Application / 05C8CB5B-9869-4D44-847B-203A237255E5 / Size Chart.app/men.plist < / p>

对于真实设备:

  

(lldb)po filePath   / private / var / mobile / Containers / Bundle / Application / 47E8272D-1289-468B-B575-4AFF9677FBF0 / Size Chart.app/men.plist

     

(lldb)po plistPath   / private / var / mobile / Containers / Bundle / Application / 47E8272D-1289-468B-B575-4AFF9677FBF0 / Size Chart.app/men.plist

为什么这个文件更新了,我可以在模拟器上看到更改的数据但不能在真正的iPhone上看到?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您无法写入与您的应用捆绑在一起的文件,因为该捆绑包是只读的。您必须将文件复制到应用程序的文档目录,然后根据需要阅读和/或更新。