I am in the process of (finally) loading my App onto an iPhone device for testing. So far I have only tested the App in the simulator. The application is data-centric and uses an SQLite database. I have created an appropriate SQLite database with sample data which I used in the simulator. How can I copy this .sqlite3 file to the actual iPhone?
This was easy with the simulator as I could just copy the .sqlite3 file into the ~/Library/Application Support/iPhone Simulator/User/Applications/{UUID}/Documents folder, but I cannot figure out how to get this into the same location on the actual iPhone device. Recreating the database from scratch on the phone is not really an option, as I have already done all the hard-yards creating the database on the Mac.
Copy files from your computer to your iOS or iPadOS app In iTunes, select the app from the list in the File Sharing section. Drag and drop files from a folder or window onto the Documents list to copy them to your device.
You can use iCloud Drive to keep your files up to date and accessible on all your devices, including Windows PCs. You can also transfer files between iPhone and other devices by using AirDrop and sending email attachments.
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.
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...
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 :)
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:&error];
}
success = [fileManager copyItemAtPath:pathBundle toPath:pathLocal error:&error];
return success;
}
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With