How to export shared container of an iOS App with Xcode6.2?
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 -
Get shared container file
[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:GROUP_NAME] URLByAppendingPathComponent:FILE_NAME].path
Get sandbox document directory path:
[[[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:FILE_NAME] path]
Copy file from Shared container to sandbox
[[NSFileManager defaultManager] copyItemAtPath:SHARED_PATH toPath:SANDBOX_PATH error:nil]
For Swift 4 - 5 add this to 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)
Then download the app container using xcode.