Search code examples
iphoneiosphotos

deleting image I've saved before exiting the program


I'm creating an app that uses pictures from the camera.

I ask the user to take a picture then save it in my document folder. It appears that it is not deleted when the program exits.

How and where is this best done (appdelegate perhaps)?


Solution

  • If you want to get the files deleted when application exit, I would recommend you to use the /tmp directory instead. Read the documentation of File System.

    Also if you just want to delete the files in documents directory, do it from applicationWillTerminate as follows:

        NSArray *homePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *homeDir = [homePaths objectAtIndex:0];
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        NSError *error=nil;
    
        NSArray *contents = [fileManager contentsOfDirectoryAtPath:homeDir error:nil];
    
        for (NSString *file in contents) {
            BOOL success = [fileManager removeItemAtPath:[homeDir stringByAppendingPathComponent:file] error:&error];
    
            if(!success){
                NSLog(@"couldn't delete file: %@",error);
            }
        }