ios – 如何查找Apple App Group共享目录

http://www.voidcn.com/article/p-pdryjkpj-bxy.html
 
我们目前正在开发iOS10应用程序,包括“Messages Extension”.

要在App和Extension之间共享CoreDatas持久性store.sqlite,我们正在使用共享的“Apple App Group”目录,该目录运行正常.

现在我们出于调试原因而无法找到该目录. Apps容器目录是完全空的,这是有道理的.但是如何下载我们的数据库?我们是否必须以某种方式将其以编程方式复制到可到达的地方?

把它们加起来:

>我们已经使用CoreData将model.sqlite存储在我们的共享目录中.
>一切都在运转.
>我们要归档的是将数据库下载到我们的计算机.

如果没有共享目录,我们只需使用Xcode-> Devices从设备下载App容器即可.但是当我们使用共享目录时,.sqlite数据库不在容器中.

题:
我们怎样才能将.sqlite数据库从设备下载到我们的计算机上?

 
编辑2018-10-12:更新了 Swift 4.x(Xcode 10)的代码. (旧版本保留供参考.)

在Swift 4.x中:

let sharedContainerURL :URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.etc.etc")
// replace "group.etc.etc" above with your App Group's identifier
NSLog("sharedContainerURL = (String(describing: sharedContainerURL))")
if let sourceURL :URL = sharedContainerURL?.appendingPathComponent("store.sqlite") {
    if let destinationURL :URL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("copyOfStore.sqlite") {
        try! FileManager().copyItem(at: sourceURL, to: destinationURL)
    }
}

在旧版本的Swift(可能是Swift 2.x)中:

let sharedContainerURL :NSURL? = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.etc.etc")  // replace "group.etc.etc" with your App Group's identifier
NSLog("sharedContainerURL = (sharedContainerURL)")
if let sourceURL :NSURL = sharedContainerURL?.URLByAppendingPathComponent("store.sqlite")
{
  if let destinationURL :NSURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0].URLByAppendingPathComponent("copyOfStore.sqlite")
  {
    try! NSFileManager().copyItemAtURL(sourceURL, toURL: destinationURL)
  }
}

像上面这样的东西会从应用程序组的共享容器中获取一个文件到应用程序的Documents目录.从那里,你可以使用Xcode>窗口>将设备送到您的计算机的设备.

在Info.plist文件中将UIFileSharingEnabled设置为YES后,您还可以使用iTunes文件共享从应用程序的Documents目录中检索文件,但请记住,这也会将目录的内容公开给用户.不过,应该可以用于开发/调试目的.

原文地址:https://www.cnblogs.com/itlover2013/p/14899271.html