复制到扩展的Document文件夹的文件默认是在iPhone上备份吗?

时间:2014-11-27 08:00:54

标签: ios ios8 ios-keyboard-extension

我正在为iOS 8编写一个键盘扩展。当键盘第一次启动时,sqlite数据库从bundle复制到Document文件夹(不存在该文件的先前副本):

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];

如果用户将来将此iPhone的备份恢复到新iPhone,是否会在新iPhone中恢复work.sqlite3文件?

2 个答案:

答案 0 :(得分:0)

您无法将文件写入应用程序的软件包文件夹

答案 1 :(得分:0)

取代

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

使用

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!
    }