- URLSessionTaskDelegate
- 概述
- 主题
- 一、协议方法
- ● func urlSession(URLSession, task: URLSessionTask, didReceive: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
- ● func urlSession(URLSession, task: URLSessionTask, didSendBodyData: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
- ● func urlSession(URLSession, task: URLSessionTask, needNewBodyStream: (InputStream?) -> Void)
- ● func urlSession(URLSession, task: URLSessionTask, willPerformHTTPRedirection: HTTPURLResponse, newRequest: URLRequest, completionHandler: (URLRequest?) -> Void)
- ● func urlSession(URLSession, task: URLSessionTask, didFinishCollecting: URLSessionTaskMetrics)
- ● class URLSessionTaskMetrics
- ● func urlSession(URLSession, task: URLSessionTask, didCompleteWithError: Error?)
- ● func urlSession(URLSession, task: URLSessionTask, willBeginDelayedRequest: URLRequest, completionHandler: (URLSession.DelayedRequestDisposition, URLRequest?) -> Void)
- ● func urlSession(URLSession, taskIsWaitingForConnectivity: URLSessionTask)
- 一、协议方法
- 关系
- 其他内容
URLSessionTaskDelegate
SDKs
Framework
Foundation
NSURLSessionTaskDelegate
协议定义了在使用任何类型的URLSession任务时应该实现的特定任务的委托方法。
概述
如果你正在使用下载任务,还需要实现URLSessionDownloadDelegate协议中的方法。
如果你正在使用数据或上传任务,还需要实现URLSessionDataDelegate协议中的方法。
Note
一个
NSURLSession
对象不需要delegate
。如果未分配delegate
,则使用系统提供的delegate
,并且你必须提供完成回调以获取数据。
主题
一、协议方法
● func urlSession(URLSession, task: URLSessionTask, didReceive: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
响应来自远程服务器的认证请求,从delegate
请求凭证。
Requests credentials from the delegate in response to an authentication request from the remote server.
● func urlSession(URLSession, task: URLSessionTask, didSendBodyData: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
定期通知delegate
向服务器发送主体内容的进度。
● func urlSession(URLSession, task: URLSessionTask, needNewBodyStream: (InputStream?) -> Void)
当任务需要新的请求内容流发送到远程服务器时,告诉delegate
。
● func urlSession(URLSession, task: URLSessionTask, willPerformHTTPRedirection: HTTPURLResponse, newRequest: URLRequest, completionHandler: (URLRequest?) -> Void)
告诉delegate
远程服务器请求HTTP重定向。
● func urlSession(URLSession, task: URLSessionTask, didFinishCollecting: URLSessionTaskMetrics)
告诉delegate
该会话完成了该任务的收集指标。
● class URLSessionTaskMetrics
一个NSURLSessionTaskMetrics
对象封装了会话任务的度量标准。每个对象都包含taskInterval和redirectCount以及执行任务期间所做的每个请求/响应事务的指标。
● func urlSession(URLSession, task: URLSessionTask, didCompleteWithError: Error?)
告诉delegate
任务完成了传输数据。
● func urlSession(URLSession, task: URLSessionTask, willBeginDelayedRequest: URLRequest, completionHandler: (URLSession.DelayedRequestDisposition, URLRequest?) -> Void)
告诉delegate
,一个延迟的URL会话任务现在将开始加载。
● func urlSession(URLSession, taskIsWaitingForConnectivity: URLSessionTask)
告诉delegate
,任务一直等待到合适的连接可用为止才会开始网络加载。