NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"idataBase.sqlite3"];
NSLog(@"%@",databasePath);
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"idataBase" ofType:@"sqlite3"];
NSLog(@"%@",databasePathFromApp);
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
if (!databaseAlreadyExists){
    [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

I tried to copy the database to document directory, But it returns null when i try to get the database path from main bundle and my app crashed. The database is present in app. But why it returns null?

有帮助吗?

解决方案

if you have twice .sqlite3 in your project

because may be your xCode target is "Build Phases" add the file under "Copy Bundle Resources"".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top