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

cnstoll

macrumors 6502
Original poster
Aug 29, 2010
254
0
So my goal is to write out my app's user data to a plist when the app terminates...so I went about implementing this by following an Apple tutorial... I get the feeling that this tutorial is not entirely accurate though, so I'm coming here for help.

http://tuvix.apple.com/library/ios/...rtyLists/QuickStartPlist/QuickStartPlist.html

Basically everything works great in terms of reading in from the plist in the main bundle. No issues there. But when I go to save the plist on termination in the ~/Documents folder...I get problems. Basically it doesn't create the file, and I'm not sure why. I can look in the App's ~/Documents on the simulator and it's not there, and on a device I can't actually look in the folder, but from the behavior I can tell it's not working either.

Here's the code (getWeightDictionary returns my "test" data in dictionary format with the correct keys/values):

Code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
	NSString *error;
	NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *plistPath = [rootPath stringByAppendingFormat:@"Weight.plist"];
	NSDictionary *plistDict = [weightDataController getWeightDictionary];
	NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
	
	if (plistData) {
		[plistData writeToFile:plistPath atomically:YES];
	}
	else {
		NSLog(error);
		[error release];
	}
}

Any Ideas? I changed the method from ApplicationShouldTerminate to the entersBackground one...because I can't find any definition that ApplicationShouldTerminate (mentioned in the tutorial) even exists. My guess is it's a Mac OS X method.
 
Also, for what it's worth, I tried creating an empty plist inside the mainbundle and writing out to that as well (in the same enterBackground method) and that didn't work either. I know the method got called though, because I can trace the execution through the whole thing when I exit the app.
 
Use stringByAppendingPathComponent when building paths.

Print out your path to see that it's valid.

Check the error returned from writeToFile.
 
I changed the method from ApplicationShouldTerminate to the entersBackground one...because I can't find any definition that ApplicationShouldTerminate (mentioned in the tutorial) even exists. My guess is it's a Mac OS X method.
The correct method name under iOS is applicationWillTerminate:.
 
Use stringByAppendingPathComponent when building paths.

Print out your path to see that it's valid.

Check the error returned from writeToFile.

hehe, that was it...

It was actually writing the file, it was just calling it .../Applications/blah/DocumentsWeight.plist :p

thanks
 
Yeah I changed that and that solved the problem. I believe that's what I'd intended to use in the first place but auto complete filled in the wrong method an I didn't notice :/
 
I just had same problem two days ago.
Maybe I just typed and saw "stringByAppendi", pressed <Enter> and expected it was what I want. I overlooked the name given. :D

Nothing is written to file but no error.

I solved it only after using NSLog to output value of each variable one by one.
:p
 
Also make sure that it does not take longer then 10 seconds to write out the file. If it does it will terminate with a crash log on the device.
 
I use this code when writing plist files

Code:
BOOL written = [d writeToFile:path atomically:NO];
DebugLog(written ? @"Successfully wrote plist file to %@":@"Couldn't write plist file to %@", path);

When you're on the Sim you can almost never be certain what folder your app is in so having the path in the console allows me to find the plist file easily if I need to.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.