Search code examples
iosobjective-ciphonexcodeplist

Can't update plist file on real device


In my app I want to download men.plist file, which is data storage for Pickerview, from my website and replace previous local version of that men.plist file. It works if I'm testing it on iPhone simulator, and it is working not if I am running it on real device.

Here is my code in 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];

After that in my ClothDataStorage.m class I do:

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

}

I compared filePath from ViewController and plistPath from Cloth DataStorage.m it's the same in both cases, for simulator:

(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

(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

For real device:

(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

Why this file updated and I can see the changed data on simulator but not on real iPhone? Thank you in advance.


Solution

  • You can't write to a file bundled with your app, as the bundle is read-only. You'll have to copy the file to the app's Documents directory, then read and or update it as needed.