How can I dele开发者_StackOverflow社区te a folder and its subfolders in Objective-C / C on iOS?
You can use NSFileManager
:
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:pathToFolder error:nil];
This question is similar to this. You can check the link or follow the answer I've provided: You can get document directory by using this:
NSString *directoryPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
** Remove full directory path by using this:
BOOL success = [fileManager removeItemAtPath:directoryPath error:nil];
if (!success) {
NSLog(@"Directory delete failed");
}
** Remove the contents of that directory using this:
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:directoryPath]) {
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:directoryPath];
NSString *documentsName;
while (documentsName = [dirEnum nextObject]) {
NSString *filePath = [directoryPath stringByAppendingString:documentsName];
BOOL isFileDeleted = [fileManager removeItemAtPath:filePath error:nil];
if(isFileDeleted == NO) {
NSLog(@"All Contents not removed");
break;
}
}
NSLog(@"All Contents Removed");
}
** You can edit directoryPath
as per your requirement.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths[0] stringByAppendingPathComponent:@"Videos"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
if (!success) {
NSLog(@"Directory delete failed");
}
}
精彩评论