Deleting all the files in the iPhone sandbox (documents folder)?

前端 未结 7 2110
清歌不尽
清歌不尽 2020-11-28 05:50

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?

7条回答
  •  隐瞒了意图╮
    2020-11-28 06:31

    Code did not work with IOS 7 and Xcode 5 so edited to work with my app. Big credits to @Ole Begemann and @pablasso.

    -(void)EmptySandbox
    {
        NSFileManager *fileMgr = [[NSFileManager alloc] init];
        NSError *error = nil;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
    
        while (files.count > 0) {
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
            if (error == nil) {
                for (NSString *path in directoryContents) {
                    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
                    BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
                    files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
                    if (!removeSuccess) {
                        // Error
                    }
                }
            } else {
                // Error
            }
        }
    }
    

提交回复
热议问题