Search code examples
iositunesfile-sharing

How to delete a file from iTunes file sharing folder iOS


I have no problem with getting the path of the files that are located in the iTunes file sharing folder.. What I want is to delete them completely when the delete button is hit..

One more question.. is it possible to distinguish the file extension when delete button is hit? for example if it's an avi file, then alert user that he is about to delete a movie?

Thanks...

Thanks to Yannik L. Its now working.. I was just wondering one more thing.. How can I delete files with non-english charachers..?


Solution

  • The iTunesFileSharing folder is simply the document folder. You can retrieve the path by executing this code:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *folderPath = [paths objectAtIndex:0];
    

    Then you can run through the files available into the document folder and test their extension to check if they are AVI files:

    - (BOOL)existsMovieFilesAtPath:(NSString *)folderPath
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        NSArray *contents = [fileManager contentsOfDirectoryAtPath:folderPath error:&error];
    
        if (error == nil)
        {
            for (NSString *contentPath in contents)
            {
                NSString *fileExt = [contentPath pathExtension];
    
                // If the current file is an AVI file
                if ([fileExt isEqualToString:@"avi"])
                {
                    return YES;
                }
            }
        }
    
        return NO;
    }
    

    And to delete the files you can make the same things:

    - (BOOL)deleteFilesAtPath:(NSString *)folderPath
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        NSArray *contents = [fileManager contentsOfDirectoryAtPath:folderPath error:&error];
    
        if (error == nil)
        {
            for (NSString *contentPath in contents)
            {
                [fileManager removeItemAtPath:contentPath error:NULL];
            }
        }
    
        return NO;
    }