views:

63

answers:

2

Hi friends,

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 application.

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.

A: 

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://iphonedevelopertips.com/multitasking/how-to-prevent-your-application-from-being-placed-into-the-background.html

Anil Sivadas
Thanks for your answer.
Pugal Devan
+1  A: 

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.

cduhn
+1, Thanks a lot. Now i have used NSCachesDirectory(instead of NSDocumentDirectory) in my apps. It works fine.
Pugal Devan