如何使用Xcode6.2导出iOS应用程序的共享容器?

时间:2015-04-19 08:01:42

标签: ios iphone watchkit xcode-6.2

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

Screenshot from Xcode

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

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

6 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:2)

实际设备的解决方法 -

我通常暂停执行我的应用程序,然后在 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]错误:nil]

上面可能看起来很复杂,但如果你突破上述情况,我们会做三件事 -

  1. 获取共享容器文件

      

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

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

      

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

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

      

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

答案 2 :(得分: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中 - >转到这样我直接进入该文件夹。这在模拟器中工作正常,但我不认为您能够从Mac访问iPhone的共享容器组。

答案 3 :(得分:1)

将数据从组容器复制到文档目录。然后使用xcode下载app容器。

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

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

func containerURL(forSecurityApplicationGroupIdentifier groupIdentifier: String) -> URL?

答案 4 :(得分:0)

我发现的最佳解决方案是创建设备备份,然后使用iExplorer探索备份。有很多AppDomainGroup-*个文件夹。虽然由于某些原因并非所有文件都备份(可能是因为在备份期间应用了要忽略的文件的设置)。

答案 5 :(得分:0)

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

import React, { useState, useEffect }  from "react";

export default function ResourceForField({location}) {
  const [ resources, setResources ] = useState([]);

  useEffect(() => {
    if (location && Array.isArray(location.resources)) {
      setResources(location.resources)
    }
  }, [location]);

    return (
      <div>
        {resources.map(res => (
          <div>test</div>
        ))}
      </div>
    );
}

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