我想用我预先填充的sqlite数据库发送我的ios应用程序(用swift编写),该数据库将通过与服务器同步来更新.在android我可以将数据库从assests文件夹移动到数据库和volla,但在ios中我很安静,我怎么能实现这一点?
谢谢
在iOS中,您可以将数据库添加到项目支持文件中.这将添加二进制文件,然后您可以访问并复制它.
要从NSBundle获取预先填充的数据库的位置:
let databasePath = NSBundle.mainBundle().URLForResource("databaseName", withExtension:"sqlite3");
Run Code Online (Sandbox Code Playgroud)
使用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);
Run Code Online (Sandbox Code Playgroud)
最后复制文件:
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.
}
Run Code Online (Sandbox Code Playgroud)