MatterGoal
MatterGoal

Reputation: 16430

Write in Main Bundle directory. Is it allowed?

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?

Upvotes: 5

Views: 5114

Answers (2)

Thomas Clayson
Thomas Clayson

Reputation: 29925

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

Upvotes: 2

jrturton
jrturton

Reputation: 119242

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.

Upvotes: 3

Related Questions