Friday, December 10, 2010

Where is iPhone Simulator NSTemporaryDirectory

Sometimes I need to save temporary files. The problem is cleaning up afterwards: application might crash during debug sessions leaving files, which are accessed on following launch thus messing up normal expected application behaviour.

iOS provides application specific temporary folder. The benefit in using this is that even if the worst case happens and your app fails to clean up temporary files for some reason (try to view 65 MB image, for example), application temporary folder is automatically cleaned out every 3 days. Your application is thus not able to leave behind "zombie" files, which would fill up device harddisk until all available space is used!

NSString *fileName = @"note.txt";
NSString *tempFilePath = [NSString stringWithFormat:@"%@%@",
    NSTemporaryDirectory(),
    fileName];
During development you want to check that temporary files are created and deleted, as planned. Easiest way is to use iPhone simulator and keep Finder window open for NSTemporaryDirectory. You can find it using:
NSLog(@"%@", self.dataPath);
Just beware that whatever you put there will be available even after application closing and relaunch as well as system reboot - for about 3 days.

No comments:

Post a Comment