Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a directory iOS?

Okay,

So I have a Cydia app that I need to update. I am aware with Cydia apps that they don't have a Documents folder, so you have to make one. And here's how I made it before in iOS 4 (which doesn't work on iOS 5):

mkdir("/var/mobile/Library/APPNAME", 0755);
mkdir("/var/mobile/Library/APPNAME/Documents", 0755);

NSString *foofile = @"/var/mobile/Library/APPNAME/Documents/database.db";
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

if (fileExists == TRUE) {
    NSLog(@"already exists");
} else {
    NSLog(@"doesn't exists");
    NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease];
    NSError *error;
    NSString *documentDBFolderPath = @"/var/mobile/Library/APPNAME/Documents/database.db";

    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];

}

I also included code that copies the database file to that folder, too. That doesn't work (even when I create the folder manually via SSH).

Please help! Thanks.

like image 896
iosfreak Avatar asked Nov 06 '11 20:11

iosfreak


People also ask

Can I create a folder in iPhone files?

Download Documents for free and open the app. Tap the purple plus button at the bottom. Select Create Folder.

How do I create a directory in Swift?

Swift – Create Directory at Specific Path To create a Directory at specific path in Swift, call createDirectory() function on the file manager object. The following code snippet creates Directory at path “/ab/cd/”. withIntermediateDirectories: true creates intermediate directories if not present.


3 Answers

Try using createDirectoryAtURL:withIntermediateDirectories:attributes:error:.

NSFileManager Class Reference:

createDirectoryAtURL:withIntermediateDirectories:attributes:error:

Creates a directory with given attributes at the specified path.

Parameters

url - A file URL that specifies the directory to create. If you want to specify a relative path, you must set the current working directory before creating the corresponding NSURL object. This parameter must not be nil.

createIntermediates - If YES, this method creates any non-existent parent directories as part of creating the directory in url. If NO, this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory.

attributes - The file attributes for the new directory and any newly created intermediate directories. You can set the owner and group numbers, file permissions, and modification date. If you specify nil for this parameter or omit a particular value, one or more default values are used as described in the discussion. For a list of keys you can include in this dictionary, see “Constants” (page 54) section lists the global constants used as keys in the attributes dictionary. Some of the keys, such as NSFileHFSCreatorCode and NSFileHFSTypeCode, do not apply to directories.

error - On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value
YES if the directory was created or already exists or NO if an error occurred.

like image 43
chown Avatar answered Sep 22 '22 15:09

chown


Here is the method I made to create directories

-(void)createDirectory:(NSString *)directoryName atFilePath:(NSString *)filePath
{
    NSString *filePathAndDirectory = [filePath stringByAppendingPathComponent:directoryName];
    NSError *error;

    if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error])
    {
        NSLog(@"Create directory error: %@", error);
    }
}
like image 152
syclonefx Avatar answered Sep 24 '22 15:09

syclonefx


Check NSFileManager's class reference. To create folders you need createDirectoryAtPath:withIntermediateDirectories:attributes:error:

like image 3
Jef Avatar answered Sep 21 '22 15:09

Jef