Search code examples
iphonedirectorynew-operatordocumentnsfilemanager

Create a folder inside documents folder in iOS apps


I just want to create new folders in the documents folder of my iPhone app.

Does anybody know how to do that?

Appreciate your help!


Solution

  • I do that the following way:

    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder