带有预填充 sqlite 数据库的 iOS 发布应用程序

标签 ios swift sqlite

<分区>

我想将我的 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/

相关文章:

ios - RxSwift 3.1 UITableView 数据源不工作

ios - TableView单元格不断改变顺序

swift - swift 中的不等式运算符(!= 等效)

android - 如何使用 views.py 捕获从 android 发送到 Django 服务器的响应?

ios - 不能形成 Range with end < start

Swift SKSpriteNode绘制顺序

ios - 如何快速避免 AFNetworking 导致的内存泄漏

android - Ormlite Android 批量插入

java - android sqlite接口(interface)可以在android外使用吗

ios - 移除 UISegmentedControl 的边缘