Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ryMac

macrumors newbie
Original poster
Jun 11, 2009
4
0
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:
//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
Code:
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?
 
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).
 
If mainbundle is readonly then to solve this problem we should create another bundle right? can anyone share some code snippet?
 
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.

Seriously it's not rocket science. If you have not read all of the iPhone Application Programming Guide stop writing code right now and read it. It tells you how to do pretty much every basic operation.
 
I ended up just writing my value to NSUserDefaults. That worked fine.

Here is the code for that:

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

Thanks for responding.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.