<分区>
我想将我的 ios 应用程序(用 swift 编写)与我预先填充的 sqlite 数据库一起发布,该数据库将通过与服务器同步进行更新。在 android 中,我可以将 db 从 assests 文件夹移动到数据库和 volla,但在 ios 中,我完全不知道如何实现这一点?
谢谢
<分区>
我想将我的 ios 应用程序(用 swift 编写)与我预先填充的 sqlite 数据库一起发布,该数据库将通过与服务器同步进行更新。在 android 中,我可以将 db 从 assests 文件夹移动到数据库和 volla,但在 ios 中,我完全不知道如何实现这一点?
谢谢
最佳答案
在 iOS 中,您可以将数据库添加到项目支持文件中。这将与二进制文件一起添加,然后您可以访问并复制它。
从 NSBundle 获取预填充数据库的位置:
let databasePath = NSBundle.mainBundle().URLForResource("databaseName", withExtension:"sqlite3");
使用 NSFileManager 获取文档目录的 URL :
//Should have an error pointed in case there is an error.
let documentsDirectory = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain:NSSearchPathDomainMask.UserDomainMask, url:nil, shouldCreate:false, error:nil);
最后复制文件:
if let source = databasePath, destination = documentsDirectory
{
destination = destination.URLByAppendingPathComponent("database.sqlite3")
var result = NSFileManager.defaultManager().copyItemAtURL(source, toURL:destination, error:nil)
//Should have an error pointer, check that the result is true. If not check error.
}
关于带有预填充 sqlite 数据库的 iOS 发布应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670219/