I have stored the data into the plist(documents directory) and retrieved the data from the plist and displayed in the table view. Now i want to remove the plist, when i closed my application. How can i do that?. I have removed plist in my controller. But how can i remove the plist after i closed my applicat开发者_开发百科ion.
For Eg :
FirstViewController.m:
-(NSString *) saveFilePath::(NSString *)tagString
{
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strPath = [[arr objectAtIndex:0] stringByAppendingPathComponent:tagString];
return strPath;
}
If i want to remove the plist in my controller, i have used this code,
[[NSFileManager defaultManager] removeItemAtPath:[self saveFilePath:tagString] error:NULL]; (Where do i put this code into my delegate method?)
But i want to remove the plist, when my application is closed. Because i have used to plist in my controller temporarily only. So i want to remove it? Is any possible to remove plist when i closed my apps?
Please guide me!
Thanks.
Put your code in the applicationWillTerminate method inside Appdelegate class.
- (void)applicationWillTerminate:(UIApplication *)application {
[[NSFileManager defaultManager] removeItemAtPath:[self saveFilePath:tagString] error:NULL];>
}
Also, applicationWillTerminate will not get called on multi-task supporting devices. So you have to add the UIApplicationExitsOnSuspend key on your info.plist. More information on this
http://iosdevelopertips.com/multitasking/how-to-prevent-your-application-from-being-placed-into-the-background.html
In addition to what Anil said, consider using NSCachesDirectory instead of NSDocumentDirectory. The contents of NSDocumentDirectory get backed up when the user syncs his phone. NSCachesDirectory is intended to be used for temporary files like yours, so it doesn't get backed up when the user syncs.
精彩评论