How do I add files to the resources folder in XCode?

19,918

Solution 1

you have to start adding your db in your project-xcode, so it will be added in your bundle folder, where you can find it via code:

[NSBundle mainBundle]

It's the only folder where you can add files via xcode when you "build" your app (eventually with subfolders, but not "system" folders as "documents") now you just need to keep in mind that the main bundle folder is just "read only", so you cant use your db there with write privileges. So the normal way is:

1) when you wanna use your db, check via code if it exists in the app:documents folder. Of course the first time it doesn't, so

2) copy it from the main bundle

- (BOOL)transferDb {
    NSError **error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourData.db"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"preferenze" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:path error:error];
        return YES;
    }
    return NO;
}

3) use it (r/w) in the documents folder

ps: and (of course) keep in mind that when you use/edit/write the db in iPhone/simulator, maybe adding records, the one in the main bundle and of course the one in your mac/project won't be updated, no records will be added to it, so if for any reason you delete your app on iPhone/simulator (or "clean all targets" by the xCode "build" menu) the check/copy method will copy the "virgin" db in the documents folder again, so you will loose all your changes...

Solution 2

Be careful with the amount of data you are putting into the Documents folder, this is meant for user data and since this data will be backed up using iCloud Apple have limited the amount of data an app can store and use in the Documents folder.

My app was rejected for using a 6MB SQLite database in this way. Instead copy it to the caches directory: NSCachesDirectory.

Or prevent the file from being backed up: https://developer.apple.com/library/ios/#qa/qa1719/_index.html

Share:
19,918
Gazzer
Author by

Gazzer

Updated on June 10, 2022

Comments

  • Gazzer
    Gazzer almost 2 years

    I want to add a sqlite database to XCode 4 (applies to XCode 3 too). Tutorials state adding the .db file to the resources folder, and I suppose this gets copied to ~/Library/Application Support/iPhone Simulator/4.2/Applications/{some-id}/Documents/ during build where you can find the file with NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) etc.

    However, XCode 4 doesn't have a visible resources folder.

    I've tried adding a file with the Add File... command, and then it appears in Targets > AppName > Copy Bundle Resources, but always an empty .db file appears in the above documents folder (which I then manually replace - obviously not the correct approach!)

    (due to the nature of the data I'm sticking with sqlite over CoreData)