简体   繁体   English

如何找到Apple App Group共享目录

[英]How to find Apple App Group shared directory

We are currently developing an iOS10 app, including "Messages Extension". 我们目前正在开发一个iOS10应用,包括“ Messages Extension”。

To share CoreDatas persistant store.sqlite inbetween App and Extension, we are using a shared "Apple App Group" directory, which is working fine. 要在App和Extension之间共享CoreDatas持久性store.sqlite ,我们使用共享的“ Apple App Group”目录,该目录运行良好。

Now we have to get our hands on the store for debug reasons and are unable to find the directory. 现在,由于调试原因,我们必须动手使用存储,并且无法找到目录。 The Apps container directories are completely empty, which makes sense. Apps容器目录完全为空,这很有意义。 But how to download our database? 但是如何下载我们的数据库? Do we have to somehow copy it programmatically to a reachable place? 我们是否必须以某种方式以编程方式将其复制到可以到达的地方?

To sum it up: 把它们加起来:

  • We already use CoreData which stores model.sqlite in our shared directory. 我们已经使用CoreData将model.sqlite存储在共享目录中。
  • Everything is up and running. 一切正常。
  • What we want to archive is to download the database to our computer. 我们要存档的是将数据库下载到我们的计算机。

Without a shared directory we can simply download the App container from the device, using Xcode->Devices. 没有共享目录,我们只需使用Xcode-> Devices从设备下载App容器即可。 But as we do use a shared directory, the .sqlite database is not within the container. 但是,由于我们确实使用共享目录,因此.sqlite数据库不在容器内。

Question: How can we download the .sqlite database from the device to our computer? 问题:如何将.sqlite数据库从设备下载到计算机?

EDIT on 2018-10-12: Updated code for Swift 4.x (Xcode 10) . 于2018-10-12编辑:更新了Swift 4.x(Xcode 10)的代码。 (Older version retained for reference.) (较旧的版本保留作为参考。)

In Swift 4.x: 在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)
    }
}

In older version of Swift (probably Swift 2.x): 在旧版的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)
  }
}

Something like the above will get a file from the app group's shared container to the app's Documents directory. 类似于上面的内容将从应用程序组的共享容器中获取文件到应用程序的Documents目录。 From there, you could use Xcode > Window > Devices to get it to your computer. 从那里,您可以使用Xcode> Window> Devices将其发送到计算机。

You could also use iTunes file sharing to retrieve the file from the app's Documents directory after setting UIFileSharingEnabled to YES in the Info.plist file, but bear in mind that this will expose the directory's contents to the user as well. Info.plist文件UIFileSharingEnabled设置为YES后,您也可以使用iTunes文件共享从应用程序的Documents目录中检索文件,但请记住,这也会向用户公开目录的内容。 Should be okay for development/debugging purposes, though. 不过,对于开发/调试而言应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM