ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用
<p><p>我在一个 Action 扩展 (<code>ActionRequestHandler</code>) 中开始下载,如下所示:</p>
<pre><code>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
}
</code></pre>
<p>然后下载由iOS在后台处理。</p>
<p>我现在想在我的主应用程序 <code>AppDelegate</code> 中处理完成的下载,因为这是下载完成时 iOS 调用的内容。</p>
<pre><code>func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
print("handleEventsForBackgroundURLSession")
urlSessionBackgroundCompletionHandler = completionHandler
}
</code></pre>
<p>此方法在预期的一段时间后在后台被调用。</p>
<p>我的 <code>AppDelegate</code> 还实现了 <code>URLSessionDelegate</code> 和 <code>URLSessionDownloadDelegate</code> 来处理下载更新。</p>
<p>特别有趣的是</p>
<pre><code>func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
print("urlSessionDidFinishEvents")
self.urlSessionBackgroundCompletionHandler?()
self.urlSessionBackgroundCompletionHandler = nil
}
}
</code></pre>
<p>和</p>
<pre><code>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)")
}
}
</code></pre>
<p><code>urlSessionDidFinishEvents</code> 和 <code>didFinishDownloadingTo</code> <strong>在后台调用 <code>handleEventsForBackgroundURLSession</code> 后都不会被调用</strong>。只有在将应用重新启动到前台后,才会调用委托(delegate)方法。</p>
<p>为什么他们没有接到电话,我该怎么做才能解决这个问题?</p>
<p>我尝试在 <code>handleEventsForBackgroundURLSession</code> 中创建 <code>URLSession</code>,如下所示:</p>
<pre><code>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
}
</code></pre>
<p>但是这并没有解决问题。</p>
<p>在您问之前:是的,我正在真实设备上进行测试,因为模拟器在后台任务处理方面存在问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我的代码实际上是正确的。只是我的调试方式是问题所在。在使用控制台日志记录而不是通过 Xcode 进行调试后,它工作正常。</p>
<p>见 <a href="https://forums.developer.apple.com/message/263807#263807" rel="noreferrer noopener nofollow">https://forums.developer.apple.com/message/263807#263807</a>有关如何使用后台 urlsession 调试的更多详细信息。</p></p>
<p style="font-size: 20px;">关于ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46390593/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46390593/
</a>
</p>
页:
[1]