Skip to content

Instantly share code, notes, and snippets.

@tuxcanfly
Created September 9, 2011 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxcanfly/1205986 to your computer and use it in GitHub Desktop.
Save tuxcanfly/1205986 to your computer and use it in GitHub Desktop.
App delegate to copy sqlite database for iOS app
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users
// phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users
// filesystem
success = [fileManager fileExistsAtPath:databasePath];
NSLog(@"Hello World!");
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to
// the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:databaseName];
NSString *masterPathFromApp = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:masterName];
// Create the database folder structure
[fileManager createDirectoryAtPath:databasePath
withIntermediateDirectories:YES attributes:nil error:NULL];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databaseFile
error:nil];
// Copy the Databases.db from the package to the appropriate place
[fileManager copyItemAtPath:masterPathFromApp toPath:masterFile
error:nil];
[fileManager release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Copy over the database if it doesn't exist
// Setup some globals
databaseName = @"0000000000000001.db";
masterName = @"Databases.db";
// Get the path to the Library directory and append the databaseName
NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains
(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDir = [libraryPaths objectAtIndex:0];
// the directory path for the Databases.db file
masterPath = [libraryDir stringByAppendingPathComponent:@"WebKit/Databases/"];
// the directory path for the 0000000000000001.db file
databasePath = [libraryDir stringByAppendingPathComponent:@"WebKit/Databases/file__0/"];
// the full path for the Databases.db file
masterFile = [masterPath stringByAppendingPathComponent:masterName];
// the full path for the 0000000000000001.db file
databaseFile = [databasePath
stringByAppendingPathComponent:databaseName];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
return [ super application:application didFinishLaunchingWithOptions:launchOptions ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment