sha*_*ght 5 ios swift ios-app-extension
我们目前正在开发iOS10应用程序,包括"Messages Extension".
为了在store.sqliteApp和Extension之间共享CoreDatas ,我们使用共享的"Apple App Group"目录,该目录运行正常.
现在我们出于调试原因而无法找到该目录.Apps容器目录是完全空的,这是有道理的.但是如何下载我们的数据库?我们是否必须以某种方式将其以编程方式复制到可到达的地方?
把它们加起来:
如果没有共享目录,我们只需使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
在旧版本的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)
}
}
Run Code Online (Sandbox Code Playgroud)
像上面这样的东西会从应用程序组的共享容器中获取一个文件到应用程序的Documents目录.从那里,您可以使用Xcode> Window> Devices将其连接到您的计算机.
你也可以使用iTunes的文件共享设置后检索从应用程序的文件目录中的文件UIFileSharingEnabled到YES了在Info.plist文件中,但记住,这将暴露目录的内容给用户也是如此.不过,应该可以用于开发/调试目的.
| 归档时间: |
|
| 查看次数: |
4404 次 |
| 最近记录: |