Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Alex said, you have to add your database file as a resource to your project. This will instruct Xcode to bundle your database file in your .app. That file however is read-only, it's up to you to copy it over to your apps document folder (on the device or simulator, same thing), where it becomes writable basically. </p> <p>Here's the code that I use for this kind of thing below. The nice thing about this code is that it will automatically refresh the writable copy in your apps document folder whenever you change the "main" copy bundled in the .app. Use 'pathLocal' to open your (writable) database...</p> <p>The function below returns YES when the operation was successful (whether a copy was needed or not). You're free to change that with whatever suits you :)</p> <pre><code>NSString *pathLocal, *pathBundle; // Automatically copy DB from .app bundle to device document folder if needed - (BOOL)automaticallyCopyDatabase { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; pathLocal = [documentsDir stringByAppendingPathComponent:@"mydb.sqlite"]; pathBundle = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *localAttr = [fileManager fileAttributesAtPath:pathLocal traverseLink:YES]; BOOL needsCopy = NO; if (localAttr == nil) { needsCopy = YES; } else { NSDate *localDate; NSDate *appDBDate; if (localDate = [localAttr objectForKey:NSFileModificationDate]) { NSDictionary *appDBAttr = [fileManager fileAttributesAtPath:pathBundle traverseLink:YES]; appDBDate = [appDBAttr objectForKey:NSFileModificationDate]; needsCopy = [appDBDate compare:localDate] == NSOrderedDescending; } else { needsCopy = YES; } } if (needsCopy) { NSError *error; BOOL success; if (localAttr != nil) { success = [fileManager removeItemAtPath:pathLocal error:&amp;error]; } success = [fileManager copyItemAtPath:pathBundle toPath:pathLocal error:&amp;error]; return success; } return YES; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload