How can you download a file from the web to a shared container while using an action extension?
class ActionViewController: UIViewController, URLSessionDownloadDelegate {
var downloadTask: URLSessionDownloadTask!
var backgroundSession: URLSession!
override func viewDidLoad() {
super.viewDidLoad()
let config = URLSessionConfiguration.background(withIdentifier: "com.flaresoftware.scroller.bgsession")
backgroundSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
}
@IBAction func addButtonPressed(_ sender: Any) {
startDownload()
}
func startDownload() {
let url = URL(string: "http://example.com/file.xml")
downloadTask = backgroundSession.downloadTask(with: url!)
downloadTask.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let fileManager = FileManager.default
let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.flaresoftware.scroller")
let destPath = NSURL(fileURLWithPath: (groupURL?.path)!).appendingPathComponent("file.xml")
do {
try fileManager.moveItem(at: location, to: destPath!)
print("App group: \(groupURL?.path)")
} catch {
print("\n Save to shared container error \n")
}
}
@IBAction func done() {
self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
}
I've been experimenting with this code, it works fine in my container app but doesn't work in the extension.