views:

107

answers:

2

Heey,

I'm writing some data to a plist file but it fails on the device but not in the simulator. Im kinda lost, dont know where to look for a solution.

This is my code:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:nameField.text forKey:@"name"];
[dict setObject:emailField.text forKey:@"email"];
BOOL ret = [dict writeToFile:[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"]] atomically:YES];
NSLog(@"%d",ret);
[dict release];

The log returns 0 on the device, but 1 in the simulator.

Can somebody help me out. Thanks

A: 

Why are you trying to write in resource directory? Have you checked this?

taskinoor
+1  A: 

This fails on the device because you cannot write to the application's bundle folder, only to the /tmp, Documents, and Cache folders. If users were able to modify the bundle, the digital signature for the app would no longer be valid and no one would be able to reliably obtain your intact app from the App Store and run it.

This works on the simulator, however, because your operating system will let you write to pretty much anywhere in your home folder, including the application bundle folder. OS X functionality is not restricted in the same way iOS's is restricted.

This is part of iOS's sandbox approach to security. The solution is to write to the /tmp, Documents or Cache folder, instead.

Alex Reynolds
Thanks manworked great
Mats Stijlaart