Created
July 4, 2013 07:01
-
-
Save Marlunes/5925492 to your computer and use it in GitHub Desktop.
ADDING SQLite DATABASE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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