5

I was pretty sure that writing in the main Bundle isn't possible in iOS ... for example an operation like :

NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
.....something
[xmlData writeToFile:path atomically:YES];

Why does the first example in Apple's documentation use this exact code?

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
MatterGoal
  • 16,038
  • 19
  • 109
  • 186

2 Answers2

3

The example is for OS X, which isn't quite as strict with permissions as iOS.

I'd be surprised if you are able to do that for much longer (if you can at all now) in a Mac App Store application bundle, though.

It could be worth filing a bug regarding the documentation.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • It happens sometimes to find documentation for OSX in iOS docs... but this time it surprised me... it would be a terrible error because as @omz says in simulator it works great! and a developer could erroneously think to use this method in its app... – MatterGoal Dec 21 '11 at 12:31
  • Another reason why testing on the device is so important! – jrturton Dec 21 '11 at 12:33
2

That's not a link to the main bundle. That's a path to the resources folder, and a plist within that folder.

Hence the function name pathForResource...

Everything in the main bundle is cryptographically signed when you compile the app. The resources folder isn't though. You can write to and from that freely.

for @jrturton

// we need to get the plist data...
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Symptoms" ofType:@"plist"];
    NSMutableArray *dataArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];

    // add a new entry
    NSDictionary *addQuestion = [[NSDictionary alloc] initWithObjectsAndKeys:@"Blank.png",@"Icon",
                                 [NSString stringWithFormat:@"%i",r],@"ID",
                                 [titleTextField text],@"Title",
                                 [questionTextField text],@"Text",
                                 qnType,@"Type",
                                 @"1",@"Custom",
                                 [NSArray arrayWithObjects:@"Yes",@"No",nil],@"Values",
                                 [unitsTextField text],@"Units",
                                 nil];
    [dataArray addObject:addQuestion];
    [addQuestion release];

    // rewrite the plist
    [dataArray writeToFile:plistPath atomically:YES];
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Thus, how do i chose where store files ? Document folder as the feature to present files in iTunes... but which is the difference between Library Path and Resource path ? – MatterGoal Dec 21 '11 at 11:51
  • I'm fairly sure this is wrong, though I can't find any documentation to back me up. The resources folder is part of the main bundle and cannot be modified on the device. – jrturton Dec 21 '11 at 12:00
  • You'll get an "Operation not permitted" error if you try to do this on a device (it does work in the simulator because simulator apps aren't codesigned). – omz Dec 21 '11 at 12:03
  • @jrturton I always use the resources folder to add and modify plist files. :) – Thomas Clayson Dec 21 '11 at 12:03
  • 1
    Interesting! On a non-jailbroken device? Care to show some code? – jrturton Dec 21 '11 at 12:06
  • @jrturton added some code into my answer. That works perfectly, on non-jailbroken devices. – Thomas Clayson Dec 21 '11 at 12:33
  • disclaimer: That code isn't published on the app store (although similar code in various other apps I've created IS published). It works in ad-hoc testing releases though on devices. Another thing I use this technique for is to "cache" plist's downloaded from the internet. Its not exactly the same, but the principal (read/write) works. – Thomas Clayson Dec 21 '11 at 12:35
  • no worries. The only problem I can find while looking up the issue is that the contents will be rewritten when the app is updated, which should be fine - otherwise I don't see why the OP wouldn't be using core data. See: http://iphoneincubator.com/blog/data-management/reading-and-writing-plists – Thomas Clayson Dec 21 '11 at 12:45
  • 1
    This didn't work for me. I didn't get an error, but the plist file was not updated. Also, your code above didn't compile - missing a few asterisks. – jrturton Dec 22 '11 at 13:14
  • 2
    I was working on a simple app using xcode 6 beta for iOS 7.1 and still can't write to mainbundle paths, and it makes sense. We gotta stick to the documents folder. Above solution won't modify a plist file for iOS. – Fer Aug 27 '14 at 13:54