Skip to content

Instantly share code, notes, and snippets.

@Marlunes
Created July 4, 2013 07:01
Show Gist options
  • Save Marlunes/5925492 to your computer and use it in GitHub Desktop.
Save Marlunes/5925492 to your computer and use it in GitHub Desktop.
ADDING SQLite DATABASE
//all stuffs will be on the Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//some appdelegate codes here......
[self checkDatabaseIfExists]; //Makes sure that the app has a database
//some appdelegate codes here ......
return YES;
}
//ADD THIS METHOD
#pragma mark - Database Checker
- (void)checkDatabaseIfExists
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *dbDocumentsURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"HettichDB.sqlite"];
NSString *dbDocumentsPath = [dbDocumentsURL path];
NSString *dbBundlePath = [[NSBundle mainBundle] pathForResource:@"MyDBName" ofType:@"sqlite"];
// copy new sqlite database from bundle to documents directory
if (dbBundlePath) {
NSError *error = nil;
[fileManager copyItemAtPath:dbBundlePath toPath:dbDocumentsPath error:&error];
if (!error)
NSLog(@"New database installed!");
}
}
//ADD THIS ONE IF NOT PRESENT IN APPDELEGATE
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment