Search code examples
iosiphoneios7nsfilemanagernsfilecoordinator

How to create a folder in iOS application Home Page


Is there is any tutorial to explain folder creation in iOS app?

In My application there is a home page contain different type of document. I want to arrange documents in folder with respect to the type of documents. can any one help?


Solution

  • Use this method to create folders in your Application's Document directory folder:

    - (void)createFolderWithName:(NSString *)aName {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                    NSUserDomainMask,
                                                                    YES) lastObject];
        NSString *folder = [documentsDirectory stringByAppendingPathComponent:aName];
    
        // Create the folder if necessary
        BOOL isDir = NO;
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        if (![fileManager fileExistsAtPath:folder
                           isDirectory:&isDir] && isDir == NO) {
            [fileManager createDirectoryAtPath:folder
               withIntermediateDirectories:NO
                                attributes:nil
                                     error:nil];
        }
    }
    

    Update:

    In order to list all folders in your document directory you need to first get list of all the files/folders and then use a UITableView to display them, when user clicks on a sub-folder then you repeat the process with updated path. For details go through this Source Code