URLSessionDownloadDelegate didFinishDownloadingTo不在后台调用

时间:2017-09-24 13:33:14

标签: ios background-process appdelegate nsurlsessiondownloadtask

我在动作扩展程序(ActionRequestHandler)中开始下载,如下所示:

private lazy var urlSession: URLSession = {
    let config = URLSessionConfiguration.background(withIdentifier: "de.stefantrauth.Downloader")
    config.sharedContainerIdentifier = "group.de.stefantrauth.Downloader"
    return URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
}()

private func initiateDownloadOfFileFrom(url: URL) {
    urlSession.downloadTask(with: url).resume()
    completeRequest() // this tells the system the action extension is done with its work
}

然后iOS在后台处理下载。

我现在想要在我的主应用程序AppDelegate中处理已完成的下载,因为这是iOS在下载完成时调用的内容。

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    print("handleEventsForBackgroundURLSession")
    urlSessionBackgroundCompletionHandler = completionHandler
}

此方法会在预期的一段时间后在后台调用。

我的AppDelegate还会实施URLSessionDelegateURLSessionDownloadDelegate来处理下载更新。

特别有趣的是

func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    DispatchQueue.main.async {
        print("urlSessionDidFinishEvents")
        self.urlSessionBackgroundCompletionHandler?()
        self.urlSessionBackgroundCompletionHandler = nil
    }
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    print("download finished to \(location.absoluteString)")
    do {
        let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let savedURL = documentsURL.appendingPathComponent(location.lastPathComponent)
        try FileManager.default.moveItem(at: location, to: savedURL)
        print("moved file to: \(savedURL.absoluteString)")
    } catch {
        print ("file error: \(error)")
    }
}

urlSessionDidFinishEvents在后​​台调用后,didFinishDownloadingTohandleEventsForBackgroundURLSession 都未被调用。只有在将应用程序重新启动到前台后,才会调用委托方法。

为什么他们没有被召唤,我该怎么做才能解决这个问题?

我尝试在URLSession中创建handleEventsForBackgroundURLSession,如下所示:

private func initUrlSessionWith(identifier: String) {
    let config = URLSessionConfiguration.background(withIdentifier: identifier)
    config.sharedContainerIdentifier = "group.de.stefantrauth.Downloader"
    urlSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
}

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    print("handleEventsForBackgroundURLSession")
    initUrlSessionWith(identifier: identifier)
    urlSessionBackgroundCompletionHandler = completionHandler
}

然而,这并没有解决问题。

在您提出问题之前:是的我在真实设备上进行测试,因为模拟器在后台任务处理方面存在问题。

1 个答案:

答案 0 :(得分:0)

我的代码实际上是正确的。我的调试方式就是问题所在。使用控制台日志记录而不是通过Xcode进行调试后,它运行正常。

有关如何使用后台网址会话调试的更多详细信息,请参阅https://forums.developer.apple.com/message/263807#263807