带有预先填充的sqlite数据库的iOS发货申请

前端之家收集整理的这篇文章主要介绍了带有预先填充的sqlite数据库的iOS发货申请前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用我预先填充的sqlite数据库发送我的ios应用程序(用swift编写),该数据库将通过与服务器同步来更新.在android我可以将数据库从assests文件夹移动到数据库和volla,但在ios我很安静,我怎么能实现这个?

谢谢

在iOS中,您可以将数据库添加到项目支持文件中.这将添加二进制文件,然后您可以访问并复制它.

要从NSBundle获取预先填充的数据库的位置:

  1. let databasePath = NSBundle.mainBundle().URLForResource("databaseName",withExtension:"sqlite3");

使用NSFileManager获取文档目录的URL:

  1. //Should have an error pointed in case there is an error.
  2. let documentsDirectory = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory,inDomain:NSSearchPathDomainMask.UserDomainMask,url:nil,shouldCreate:false,error:nil);

最后复制文件

  1. if let source = databasePath,destination = documentsDirectory
  2. {
  3. destination = destination.URLByAppendingPathComponent("database.sqlite3")
  4. var result = NSFileManager.defaultManager().copyItemAtURL(source,toURL:destination,error:nil)
  5.  
  6. //Should have an error pointer,check that the result is true. If not check error.
  7. }

猜你在找的Sqlite相关文章