How to make a directory iOS?
Asked Answered
S

5

29

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.

Senility answered 6/11, 2011 at 20:13 Comment(3)
What is going wrong, or what other piece of code could I use to create directories?Senility
Right, what is going wrong? You haven't told us. No error messages, no description of the failure.Illeetvilaine
@Daniel R Hicks: Nothing is happening.. After pasting from the code below, I DO get an error... Create directory error: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1c2650 {NSFilePath=/var/mobile/Library/APPNAME, NSUnderlyingError=0x1c3520 "The operation couldn’t be completed. Operation not permitted"}Senility
D
63

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);
    }
}
Dublin answered 6/11, 2011 at 20:38 Comment(2)
This is the error I get... The operation couldn’t be completed. Operation not permittedSenility
This might be what your looking for #4650988Dublin
S
5

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.

Supinate answered 6/11, 2011 at 20:30 Comment(1)
Seems Im a bit slow when answering from my iPad..., the other answer appears to be pretty much the same as this.Supinate
C
3

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

Cheese answered 6/11, 2011 at 20:20 Comment(4)
I tried that and no folder was created... I think that's only for OSx, not iOS?Senility
Have you used YES as argument of withIntermediateDirectories: ?Cheese
NSFileManager *NSFm= [NSFileManager defaultManager]; [NSFm createDirectoryAtPath:@"/var/mobile/Library/APPNAME" withIntermediateDirectories:YES attributes:nil error:nil]; [NSFm createDirectoryAtPath:@"/var/mobile/Library/APPNAME/Documents" withIntermediateDirectories:YES attributes:nil error:nil]; was the exact code i used...Senility
Assuming /var/mobile/Library/ exists, you shouldn't need the first call, just the second. Are you sure you have the correct write permissions? What error does it return?Cheese
E
1

Superb Techotopia explanation of iOS5 filesystem

Eichmann answered 22/3, 2013 at 1:4 Comment(0)
S
1

In Swift, returns true if exists or created.

func ensureDirectoryExists(path:String) -> Bool {

    if !NSFileManager.defaultManager().fileExistsAtPath(path) {
        do {
            try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error)
            return false
        }
    }
    return true
}
Seleta answered 18/7, 2016 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.