问题一 -999
被释放了,会报-999,所以可以确定URLSession.init是一个单例
问题二 掉帧
测试之前先把模拟器上的App删掉,然后Xcode关掉,重新打开Xcode编译一下代码
,再运行
Alamofire-系统URLSession中后台下载
1、开通后台下载权限,就会在下载完成的时候自己去执行func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {}
方法
2、AppDelegate
中执行self.backgroundSessionCompletionHandler = completionHandler
var backgroundSessionCompletionHandler: (() -> Void)?
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
self.backgroundSessionCompletionHandler = completionHandler
}
3、urlSessionDidFinishEvents
中执行DispatchQueue.main.async { guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let backgroundHandle = appDelegate.backgroundSessionCompletionHandler else { return } backgroundHandle() }
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
print("后台任务下载回来")
self.navigationController?.pushViewController(DetailViewController.init(), animated: true)
DispatchQueue.main.async {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let backgroundHandle = appDelegate.backgroundSessionCompletionHandler else { return }
backgroundHandle()
}
}
问题三 identifier
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
let configuration = URLSessionConfiguration.background(withIdentifier: identifier)
let session = URLSession(configuration: configuration)
}
网友评论