iOS ship application with pre populated sqlite database [closed] iOS ship application with pre populated sqlite database [closed] sqlite sqlite

iOS ship application with pre populated sqlite database [closed]


In iOS you can add the database into the Project Supporting files. This will be added in with the binary, which you can then access and copy it across.

To get the location of the pre-populated database from NSBundle:

let databasePath = NSBundle.mainBundle().URLForResource("databaseName", withExtension:"sqlite3");

Get the URL of the Documents Directory using NSFileManager:

//Should have an error pointed in case there is an error.let documentsDirectory = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain:NSSearchPathDomainMask.UserDomainMask, url:nil, shouldCreate:false, error:nil);

Finally copy the file:

if let source = databasePath, destination = documentsDirectory{   destination = destination.URLByAppendingPathComponent("database.sqlite3")   var result = NSFileManager.defaultManager().copyItemAtURL(source, toURL:destination, error:nil)   //Should have an error pointer, check that the result is true. If not check error.}


The simple way is:+ Choose the persistent URL is somewhere in your library folder/sub-folder (or documents if you want). + when you implement your persistentStoreCoordinator, first check if the .sqlite exists at the URL above. If not, copy your .sqlite file to that location, if yes, do nothing.+ init your persistent store with that url.

You have to sure that the .sqlite's datas match the DB model