OGeek|极客世界-中国程序员成长平台

标题: ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:45
标题: ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用

我在一个 Action 扩展 (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)")
    }
}

urlSessionDidFinishEventsdidFinishDownloadingTo 在后台调用 handleEventsForBackgroundURLSession 后都不会被调用。只有在将应用重新启动到前台后,才会调用委托(delegate)方法。

为什么他们没有接到电话,我该怎么做才能解决这个问题?

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

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
}

但是这并没有解决问题。

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



Best Answer-推荐答案


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

https://forums.developer.apple.com/message/263807#263807有关如何使用后台 url session 调试的更多详细信息。

关于ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390593/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4