Thursday, April 28, 2011

iPhone create folder inside documents folder

Hello,

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!

From stackoverflow
  • I do that the following way:

    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 attributes:nil]; //Create folder
    
    zanque : thanks for the reply, it works but it creates the folder "MyFolder" beside the documents folder of the app .. not inside it. don't know why actually
    Vladimir : Sorry - was a typo there - must append @"/MyFolder", not @"MyFolder" to a documents path.
    zanque : yes, i discovered that, thanks for help :)
    Andreas : You might want to change the third line to NSString *dataPath = [documentsDirectory stringByAppendingString:@"/MyFolder"]; since NSSearchPathForDirectoriesInDomains doesn't return trailing slashes.
    Kendall Helmstetter Gelner : I fixed the code to use the more correct "stringByAppendingPathComponent", which does the right thing regardless of either input string having a "/" or not.

0 comments:

Post a Comment