Search code examples
objective-ciosnsfilemanagernsbundle

NSFileManager application folders - ios


I want to create 2 folders: "Images" and "Text", to download image type files to "Image" folder, and "text" type to "Text" folder. Questions: 1)Where and how i should create them? 2)How to get path for this created folders? Please write some short example, or give url to similar example.


Solution

  • Try this: (your can learn more about NSFileManager from Apple's docs)

    NSError *error = nil;
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  //this get you to the root of your app's
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"yourfoldername"];  
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])  //Optionally check if folder already hasn't existed.
    {
    [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:&error];
    }