ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用

标签 ios background-process appdelegate nsurlsessiondownloadtask

我在 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
}

但这并没有解决问题。

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

最佳答案

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

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

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

相关文章:

ios - 如何在 Swift 3 中使用 UIWebview 将动态网页保存在缓存中

ios - 强制提前调用 iOS 设备上的后台任务到期处理程序以进行测试

ios - AppDelegate didFinishLaunchingWithOptions launchOptions - 以 NSException Swift 3.0 类型的未捕获异常终止

SwiftUI New App生命周期如何连接Facebook SDK

ios - 从这样的日期中删除时间 2016-02-10 00 :00:00

ios - 上传xml文件到服务器

ios - 我如何清理 UINavigationBar 转换历史记录?

java - Flutter FCM后台调用平台特定代码

ios - 如何以编程方式打开和关闭后台获取

ios - 3d touch swift 3 不打开viewcontroller