0

I am writing a keyboard extension for iOS 8. A sqlite database is copied from the bundle to the Document folder when the keyboard is started for the first time (not previous copy of the file exists):

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"work.sqlite3"];
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"default" 
                                                       ofType: @"sqlite3"];
[[NSFileManager defaultManager] copyItemAtPath:bundlePath 
                                        toPath:dbPath error:nil];

In case the user restores the backup of this iPhone to a new iPhone in the future, will the work.sqlite3 file be restored in the new iPhone?

ohho
  • 50,879
  • 75
  • 256
  • 383

2 Answers2

0

You can't write a file to the bundle folder of your application

soulshined
  • 9,612
  • 5
  • 44
  • 79
0

in place of

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

Use

func getTheFilePath() -> String
    {
        var url = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.groupBundleID") as NSURL?
        var path = url?.absoluteString?.stringByAppendingPathComponent("work.sqlite3") as String?
        path = path!.stringByReplacingOccurrencesOfString("file:", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

        return path!
    }
Amit Bhavsar
  • 1,273
  • 12
  • 23