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

但是,共享存储不包含在容器中,可能是因为它位于设备本身的不同路径中.
问题是如果可能的话,我们如何获得共享容器?
实际设备的解决方法 -
我通常会暂停应用程序的执行,然后在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] 路径] 错误:零]
上面可能看起来很复杂,但如果你打破了上面的规定,我们正在做三件事 -
获取共享容器文件
[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:GROUP_NAME] URLByAppendingPathComponent:FILE_NAME].path
获取沙箱文档目录路径:
[[[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:FILE_NAME] 路径]
将文件从共享容器复制到沙箱
[[NSFileManager defaultManager] copyItemAtPath:SHARED_PATH toPath:SANDBOX_PATH 错误:nil]
小智 6
对于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)
Run Code Online (Sandbox Code Playgroud)
然后使用 xcode 下载应用程序容器。
我使用此代码在控制台中打印出本地 SQLite 数据库的路径:
NSString *groupContainer = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.myapp.mycontainer"] path];
NSString *sqlitePath = [NSString stringWithFormat:@"%@/Database.sqlite", groupContainer];
NSURL *url = [NSURL fileURLWithPath:sqlitePath];
Run Code Online (Sandbox Code Playgroud)
然后,我将字符串复制并粘贴到 Finder -> Go to 中,这样我就可以直接进入该文件夹。这在模拟器中运行良好,但我认为您无法从 Mac 访问 iPhone 的共享容器组。
| 归档时间: |
|
| 查看次数: |
2214 次 |
| 最近记录: |