PDA

View Full Version : Write to .plist works on simulator, not on device




ryMac
Jun 15, 2009, 04:36 PM
I am trying to store a single an email address for an application in the plist file. It works fine on the simulator, but it doesn't work on my iPhone. Here is my code:

code to view what is in the .plist
//code to retrieve info from plist
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

NSString *emailString = [NSString stringWithFormat:@"%@", [plistData objectForKey:@"savedEmail"]];
fromEmailInput.text = emailString;
NSLog(emailString);


code to write to the .plist

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *propList = [path stringByAppendingPathComponent:@"Info.plist"];

NSMutableDictionary* plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:propList];
if (plistData) {
NSLog(@"write file was found");
[plistData setValue:fromEmailInput.text forKey:@"savedEmail"];
[plistData writeToFile:propList atomically: YES];
}


"write file was found" shows properly in the log when I run the app on my phone. Any idea why this appears to write properly on the simulator and not the phone? Anyone else run into this?



robbieduncan
Jun 16, 2009, 03:36 AM
What path are you trying to write to? You can only write to your applications sandboxed area of the filesystem on the device (and certainly not into your applications bundle).

hell1197
Jun 16, 2009, 04:33 AM
mainbundle is readonly...

dusker
Jun 16, 2009, 07:16 AM
If mainbundle is readonly then to solve this problem we should create another bundle right? can anyone share some code snippet?

robbieduncan
Jun 16, 2009, 07:22 AM
If mainbundle is readonly then to solve this problem we should create another bundle right? can anyone share some code snippet?

No. You should not create a bundle at all. You should write to your applications sandboxed area on the filesystem as clearly described in the documentation (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW6).

Seriously it's not rocket science. If you have not read all of the iPhone Application Programming Guide (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072-CH1-SW1) stop writing code right now and read it. It tells you how to do pretty much every basic operation.

ryMac
Jun 16, 2009, 10:30 AM
I ended up just writing my value to NSUserDefaults. That worked fine.

Here is the code for that:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:fromEmailInput.text forKey:@"savedEmail"];

Thanks for responding.