How to export shared container of an iOS App with Xcode6.2?

我与影子孤独终老i 提交于 2019-11-28 21:31:47

I've been told by Xcode team members at WWDC that this is not possible (at time of writing, Xcode 7.3 & 8 beta 1). I've filed a radar, which I recommend everyone dupe or comment on so we can get this functionality.

Workaround for actual device -

I generally pause the execution of my app and then run following command in lldb debugger to copy the desired file from shared container to my sandbox container and then I download the container from 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] error:nil]

Above might look complex but if you break the above, we are doing 3 things -

  1. Get shared container file

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

  2. Get sandbox document directory path:

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

  3. Copy file from Shared container to sandbox

    [[NSFileManager defaultManager] copyItemAtPath:SHARED_PATH toPath:SANDBOX_PATH error:nil]

I'm using this code to print out in the console the path to my local SQLite database:

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

I then copy and paste the string in the Finder -> Go to so I go directly to the folder. This works fine in the Simulator but I don't think you're able to access the your iPhone's shared container group from your Mac.

Copy your data from the group container to document directory. Then download the app container using xcode.

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

url here can be obtained using below function of NSFileManager

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

The best solution I've found is creating a backup of your device and then using iExplorer to explore the backup. There are bunch of AppDomainGroup-* folders. Though for some reason not all of the files there are backed up (probably because of applied settings to files to be ignored during backup).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!