Search code examples
objective-cioscore-datajailbreak

Cydia App Documents Folder Not Created


I have been working on a Core Data iOS app that works perfectly through Apple's "channels" – iOS simulator & Xcode installing, yet when I try to manually install it onto a device, the application crashes. My main goal is to put the app on Cydia.

A guide to preparing an app for Cydia I read this article, and I at the bottom it said

Appstore app has a Documents folder that is created by the installation process. Jailbroken app does not. It is up to the app to create its own folder. Should you need this type of folder, you must create this with a simple mkdir command in your applicationDidFinishLaunching function. Just add a simple function: mkdir(“/var/mobile/Library/YOURAPPNAME”, 0755); If the folder already exists, no harm done. You want to do this because the install process runs as user root and the app runs as user mobile. If Cydia does this for you then the folder will have the incorrect permissions.

I do not know much about how exactly Core Data works, but I know the Core Data "database" is stored in the Documents folder, and I now believe this is the cause of the crash of my app.

The mkdir function did not work in creating a Documents folder.

How would I go about creating a Documents folder, and how would I get it to work with Core Data, ensuring the database is loaded from this folder I created?

Thanks in advance


Solution

  • Most likely you are still using the AppStore friendly Documents path in the methods which create and load the CoreData database file (I think Xcode will put these in your AppDelegate by default).

    Check the method which loads the persistentStorageCoordinator and look for a line like this:

    NSURL *storeUrl = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"my_cool_app.sqlite"]];
    

    And make sure that docPath is "/var/mobile/Library/My_Cool_App" and not originating from the standard AppStore friendly:

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [documentPaths objectAtIndex:0];
    

    You might want to create a method which returns the proper Documents directory depending on what target you compile the app for:

    +(NSString *)documentsDirectoryPath
    {
    #ifdef JAILBREAK
    
        NSString *documentPath = @"/var/mobile/Library/My_Cool_App";
    
        if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath])
        {
            [[NSFileManager defaultManager] createDirectoryAtPath:documentPath
                                  withIntermediateDirectories:NO 
                                                   attributes:nil 
                                                        error:NULL];
        }
    
        return documentPath;
    
    #else
    
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        return [documentPaths objectAtIndex:0];
    
    #endif
    }