Create a folder inside documents folder in iOS apps

后端 未结 9 1036
一生所求
一生所求 2020-11-28 17:51

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!

9条回答
  •  無奈伤痛
    2020-11-28 18:13

    Following code may help in creating directory :

    -(void) createDirectory : (NSString *) dirName {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
    dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:dirName];
    
    NSError *error;
    
    if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]) {
        NSLog(@"Couldn't create directory error: %@", error);
    }
    else {
        NSLog(@"directory created!");
    }
    
    NSLog(@"dataPath : %@ ",dataPath); // Path of folder created
    
    }
    

    Usage :

    [self createDirectory:@"MyFolder"];
    

    Result :

    directory created!

    dataPath : /var/mobile/Applications/BD4B5566-1F11-4723-B54C-F1D0B23CBC/Documents/MyFolder

提交回复
热议问题