18

在我们的 iOS 应用程序中,我们使用共享容器在我们的主 iOS 应用程序及其扩展程序(特别是 WatchKit 扩展程序)之间共享文件,使用[NSFileManager containerURLForSecurityApplicationGroupIdentifier:]方法。出于调试目的,我们需要访问此共享容器的内容,因此我们尝试使用 Xcode 中的 Devices 窗口导出整个 App 容器:

来自 Xcode 的屏幕截图

但是,共享存储不包含在容器中,可能是因为它位于设备本身的不同路径中。

问题是我们如何才能获得共享容器,如果这可能的话?

4

6 回答 6

9

WWDC 的 Xcode 团队成员告诉我这是不可能的(在撰写本文时,Xcode 7.3 & 8 beta 1)。我已经提交了一个雷达,我建议每个人都对其进行欺骗或评论,以便我们可以获得此功能。

于 2016-06-16T22:21:22.290 回答
5

实际设备的解决方法 -

我通常会暂停我的应用程序的执行,然后在lldb 调试器中运行以下命令,将所需文件从共享容器复制到我的沙箱容器,然后从 Xcode 下载容器。

po [[NSFileManager defaultManager] copyItemAtPath:[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:GROUP_NAME] URLByAppendingPathComponent:FILE_NAME].path toPath:[[[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:FILE_NAME] path] 错误:零]

上面可能看起来很复杂,但如果你打破上面的内容,我们正在做 3 件事 -

  1. 获取共享容器文件

    [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:GROUP_NAME] URLByAppendingPathComponent:FILE_NAME].path

  2. 获取沙箱文档目录路径:

    [[[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:FILE_NAME] 路径]

  3. 将文件从共享容器复制到沙箱

    [[NSFileManager defaultManager] copyItemAtPath:SHARED_PATH toPath:SANDBOX_PATH 错误:nil]

于 2018-05-10T20:26:58.763 回答
4

对于Swift 4 - 5将此添加到 applicationDidEnterBackground(_ application: UIApplication)

var appGroupIdentifier = "XXXXXXX"
let fromURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
let docURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let toURL = docURL.appendingPathComponent(appGroupIdentifier)
try? FileManager.default.removeItem(at: toURL)
try? FileManager.default.copyItem(at: fromURL, to: toURL)

然后使用 xcode 下载应用程序容器。

于 2019-12-25T11:33:59.570 回答
1

我正在使用此代码在控制台中打印出本地 SQLite 数据库的路径:

NSString *groupContainer = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.myapp.mycontainer"] path];
NSString *sqlitePath = [NSString stringWithFormat:@"%@/Database.sqlite", groupContainer];
NSURL *url = [NSURL fileURLWithPath:sqlitePath];

然后,我将字符串复制并粘贴到 Finder -> Go to 中,这样我就直接进入了该文件夹。这在模拟器中运行良好,但我认为您无法从 Mac 访问 iPhone 的共享容器组。

于 2015-04-19T15:58:16.097 回答
1

将您的数据从组容器复制到文档目录。然后使用 xcode 下载应用程序容器。

NSFileManager.defaultManager().copyItemAtURL(url, toURL: NSURL.fileURLWithPath(AppDelegate.applicationUserDirectory()))

可以使用下面的 NSFileManager 函数获取这里的 url

func containerURL(forSecurityApplicationGroupIdentifier groupIdentifier: String) -> URL?
于 2016-05-02T13:20:06.217 回答
0

我发现的最佳解决方案是创建设备备份,然后使用 iExplorer 探索备份。有一堆AppDomainGroup-*文件夹。尽管由于某种原因,并非所有文件都已备份(可能是因为对文件应用的设置在备份期间会被忽略)。

于 2015-09-22T04:31:36.820 回答