49 votes

La suppression de tous les fichiers de l'iPhone sandbox (dossier de documents)?

Est-il un moyen facile de supprimer tous les fichiers(images) j'ai enregistré dans le dossier documents de l'application?

94voto

Ole Begemann Points 85798
NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
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];
        if (!removeSuccess) {
            // Error handling
            ...
        }
    }
} else {
    // Error handling
    ...
}

15voto

Msmit1993 Points 339

Le Code ne fonctionne pas avec IOS 7 et Xcode 5 donc modifié pour fonctionner avec mon application. Gros crédits à @Ole Begemann et @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
        }
    }
}

7voto

Luong Huy Duc Points 330
- (void)removeFile
{
    // you need to write a function to get to that directory
    NSString *filePath = [self getDirectory];
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    if ([fileManager fileExistsAtPath:filePath]) 
    {
        NSError *error;
        if (![fileManager removeItemAtPath:filePath error:&error])
        {
            NSLog(@"Error removing file: %@", error); 
        };
    }
}

Je crois que c'est plus court.

2voto

mahesh chowdary Points 198
 NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
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];
    if (!removeSuccess) {
        // Error handling
        ...
    }
}
} else {
 // Error handling
...
}

2voto

AXE Points 585

Il peut ne pas être applicable dans tous les cas, mais en général, il serait plus efficace de placer tous les fichiers dans un répertoire personnalisé à l'intérieur de Répertoire Documents et ensuite utiliser removeItemAtPath:error: supprimer ce répertoire et de le créer encore une fois. E. g.:

// Clear cache
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:cacheDirectory error:&error];
if (error)
{
    // error handling
}

// Create cache directory again
NSError *error2;
[[NSFileManager defaultManager] createDirectoryAtPath:cacheDirectory withIntermediateDirectories:YES attributes:nil error:&error2];
if (error2)
{
    // error handling
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X