问题描述
在我的应用程序中,我需要下载一个文件(大小:50mb),还需要支持后台下载。通过以下代码,我甚至可以在后台进行下载。唯一的问题是当我尝试切换应用程序(比如我打开 WhatsApp)并返回我的应用程序而不进入 iPhone 的主屏幕时,UI 没有正确更新。
weak var downloadTask: URLSessionDownloadTask?
lazy var downloadSession: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier: "MYDOWNLOADIDENTIFIER")
return URLSession(configuration: configuration,delegate: self,delegateQueue: OperationQueue.main)
}()
下载按下的事件:
downloadTask = self.downloadSession.downloadTask(with: fileUrl)
downloadTask?.resume()
func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
//UI update for progress level
}
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
logger.write("urlSessionDidFinishEvents\n")
dispatchQueue.main.async {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,let completionHandler = appDelegate.backgroundSessionCompletionHandler {
appDelegate.backgroundSessionCompletionHandler = nil
completionHandler()
}
}
}
AppDelegate.swift
func application(_ application: UIApplication,handleEventsForBackgroundURLSession identifier: String,completionHandler: @escaping () -> Void) {
backgroundSessionCompletionHandler = completionHandler
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)