- URLSessionConfiguration 三种配置模式
open class var `default`: URLSessionConfiguration { get }
open class var ephemeral: URLSessionConfiguration { get }
@available(iOS 8.0, *)
open class func background(withIdentifier identifier: String) -> URLSessionConfiguration
default
:默认模式,该模式下系统会创建一个持久化的缓存并在用户的钥匙串中存储证书。
ephemeral (短暂性的)
:系统没有任何持久性存储,所有内容的生命周期都与session相同,当session无效时,所有内容自动释放。
background
:后台模式会用一个独立线程来进行数据传输。创建session
时需要配置一个identifier
。
background
模式可以在程序挂起,退出,崩溃的情况下运行task
。可以利用identifier
标识符来恢复进。APP下次运行的时候,能够根据identifier
来进行相关的区分。
background
模式下如果用户关闭了APP,IOS 系统会关闭所有的background Session
。只有用户下次启动了APP,数据传输才会继续。
- 使用方式:
let configuration = URLSessionConfiguration.background(withIdentifier: "downloadID")
let session = URLSession.init(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
session.downloadTask(with: url) { (data, response, error) in
}.resume()
- 下载进度:
//MARK:URLSessionDownloadDelegate
// 下载进度
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print(" 写入字节: \(bytesWritten)\n 写入总字节: \(totalBytesWritten)\n 期望总字节: \(totalBytesExpectedToWrite)")
print("下载进度: \(Double(totalBytesWritten)/Double(totalBytesExpectedToWrite))\n")
}
// 下载完成
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("下载完成 - \(location)")
let locationPath = location.path
let documnets = NSHomeDirectory() + "/Documents/" + "test.mp4"
print("移动地址:\(documnets)")
//创建文件管理器
let fileManager = FileManager.default
try! fileManager.moveItem(atPath: locationPath, toPath: documnets)
}
- 后台配置
// Applications using an NSURLSession with a background configuration may be launched or resumed in the background in order to handle the
// completion of tasks in that session, or to handle authentication. This method will be called with the identifier of the session needing
// attention. Once a session has been created from a configuration object with that identifier, the session's delegate will begin receiving
// callbacks. If such a session has already been created (if the app is being resumed, for instance), then the delegate will start receiving
// callbacks without any action by the application. You should call the completionHandler as soon as you're finished handling the callbacks.
使用具有后台配置的NSURLSession的应用程序可以在后台启动或恢复,以便处理该会话中任务的完成或处理身份验证。
调用此方法时将注意会话的标识符。从具有该标识符的配置对象创建会话后,会话的委托将开始接收回调。
如果已经创建了这样的会话(例如,如果正在恢复应用程序),则委托将开始接收回调,而应用程序不会执行任何操作。
处理完回调后,应立即调用completionHandler。
//用于保存后台下载的completionHandler
var backgroundSessionCompletionHandler: (() -> Void)?
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
backgroundSessionCompletionHandler = completionHandler
}
//MARK:URLSessionDelegate
// 后台下载成功后执行该方法
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
}
网友评论