NSURLSession 所有的都在這里(二)


 

前面一篇我們說了什么?


 

      這是這個關於NSURLSession的第二篇文章,第一篇再加上這篇文章,就大概的把NSURLSession的API以及一些簡單使用我們也就說的差不多了,這篇文章總結哪些點呢?相信看顧哦第一篇文章的小伙伴都知道,在開始寫這篇文章之前我們說說上一篇文章我們寫了些什么。

      1、NSURLRequest

      2、NSURLSession

      3、NSURLSessionTask 以及它的三個子類:NSURLSessionDataTask/NSURLSessionDownloadTask/NSURLSessionStreamTask

     

      第一篇文章的飛機票在這里 NSURLSession 所有的都在這里(一)

 

      拿着一篇我們說的大概是下面這些內容了:

      1、NSURLSessionTaskDelegate

      2、NSURLSessionDataDelegate

      3、NSURLSessionDownloadDelegate

      4、NSURLSessionStreamDelegate

      5、NSURLSessionTaskTransactionMetrics

      6、NSURLSessionTaskMetrics

 

開始這篇的內容  -- delegate


      在說明之前我們先通過一張圖看看這幾個代理之間的關系:

      這些代理全都是遵循了NSURLSessionDelegate,再說上面我們提到的那些代理的API之前我們得先說說NSURLSessionDelegate這個代理,看下面它的API: 

/*
 * Messages related to the URL session as a whole
 @protocol NSURLSessionDelegate <NSObject>
 @optional
 */

/* The last message a session receives.  A session will only become
 * invalid because of a systemic error or when it has been
 * explicitly(明顯) invalidated, in which case the error parameter will be nil.
 會話失效
 通知URL會話該會話已失效。
 如果通過調用finishTasksAndInvalidate方法使會話失效,則會話將一直等待,直到會話中的最終任務完成或失敗,然后再調用此委托方法。如果您調用invalidateAndCancel方法,
 會話將立即調用此委托方法。
 對於每一個完成的后台Task調用該Session的Delegate中的URLSession:downloadTask:didFinishDownloadingToURL:(成功的話)
 和URLSession:task:didCompleteWithError:(成功或者失敗都會調用)方法做處理,以上的回調代碼塊可以在這里調用
 - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error;

 */

/* If implemented, when a connection level authentication challenge
 * has occurred, this delegate will be given the opportunity to
 * provide authentication credentials to the underlying
 * connection. Some types of authentication will apply to more than
 * one request on a given connection to a server (SSL Server Trust
 * challenges).  If this delegate message is not implemented, the
 * behavior will be to use the default handling, which may involve user
 * interaction.
 
  如果服務器要求驗證客戶端身份或向客戶端提供其證書用於驗證時,則會調用
  在蘋果開發者文檔中有這樣的說明
  If the initial handshake with the server requires a connection-level challenge (such as an SSL client certificate), NSURLSession calls either the URLSession:task:didReceiveChallenge:completionHandler: or URLSession:didReceiveChallenge:completionHandler: delegate method.
 
 響應來自遠程服務器的會話級別認證請求,從代理請求憑據。
 這種方法在兩種情況下被調用:
 1、遠程服務器請求客戶端證書或Windows NT LAN Manager(NTLM)身份驗證時,允許您的應用程序提供適當的憑據
 2、當會話首先建立與使用SSL或TLS的遠程服務器的連接時,允許您的應用程序驗證服務器的證書鏈
 如果您未實現此方法,則會話會調用其委托的URLSession:task:didReceiveChallenge:completionHandler:方法。
 注:此方法僅處理NSURLAuthenticationMethodNTLM,NSURLAuthenticationMethodNegotiate,NSURLAuthenticationMethodClientCertificate和NSURLAuthenticationMethodServerTrust身份驗證類型。對於所有其他認證方案,會話僅調用URLSession:task:didReceiveChallenge:completionHandler:方法。
 
  - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;

 */

/* If an application has received an
 * -application:handleEventsForBackgroundURLSession:completionHandler:
 * message, the session delegate will receive this message to indicate
 * that all messages previously enqueued for this session have been
 * delivered.  At this time it is safe to invoke the previously stored
 * completion handler, or to begin any internal updates that will
 * result in invoking the completion handler.
 
 如果一個應用程序收到了
 -application:handleEventsForBackgroundURLSession:completionHandler:
 消息,session委托將收到此消息指示所有消息之前進行入隊這個會話交付。這個時候是安全調用先前存儲完成處理器,或開始任何內部更新將導致調用完成處理器。
 
 告訴委托所有session里的消息都已發送。
 這個方法在我們寫后台下載的Demo中我們是會遇到的。
 - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
*/

/*
 下面是Task解析
 NSURLSessionTask是一個抽象子類,它有三個子類:NSURLSessionDataTask,NSURLSessionUploadTask和NSURLSessionDownloadTask。這三個類封裝了現代應用程序的三個基本網絡任務:獲取數據,比如JSON或XML,以及上傳和下載文件。
*/

 

      上面我們說了NSURLSessionDelegate這個代理,接下來就是我們的NSURLSessionTaskDelegate,看到Task就知道接下來的還有我們常見的三個Task代理:

      我們先看看這個:NSURLSessionTaskDelegate,然后在一個個的說明那三個Task(data,download,stream)代理。關於uploadTask由於是繼承dataTask的。

 

/*
 * Sent when the system is ready to begin work for a task with a delayed start
 * time set (using the earliestBeginDate property). The completionHandler must
 * be invoked in order for loading to proceed. The disposition provided to the
 * completion handler continues the load with the original request provided to
 * the task, replaces the request with the specified task, or cancels the task.
 * If this delegate is not implemented, loading will proceed with the original
 * request.
 *
 * Recommendation: only implement this delegate if tasks that have the
 * earliestBeginDate property set may become stale and require alteration prior
 * to starting the network load.
 *
 * If a new request is specified, the allowsCellularAccess property from the
 * new request will not be used; the allowsCellularAccess property from the
 * original request will continue to be used.
 *
 * Canceling the task is equivalent to calling the task's cancel method; the
 * URLSession:task:didCompleteWithError: task delegate will be called with error
 * NSURLErrorCancelled.
 
 告訴代理現在將開始加載延遲的URL會話任務。
 當具有延遲開始時間的后台會話任務(由earliestBeginDate屬性設置)准備就緒時,將調用此方法。只有在等待網絡負載時請求可能變陳舊並需要被新請求替換時,才應實現此委托方法。
 為了繼續加載,委托人必須調用完成處理程序,並傳遞一個處理方式來指示任務應該如何進行。傳遞NSURLSessionDelayedRequestCancel處置等效於直接調用任務的取消。
 
   - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
   willBeginDelayedRequest:(NSURLRequest *)request
   completionHandler:(void (^)(NSURLSessionDelayedRequestDisposition disposition, NSURLRequest * _Nullable newRequest))completionHandler
   API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
 */

/* 告訴代理,在開始網絡加載之前,任務正在等待,直到合適的連接可用。
 如果NSURLSessionConfiguration的waitsForConnectivity屬性為true並且沒有足夠的連接,則調用此方法。 代表可以利用這個機會來更新用戶界面;
 例如通過呈現離線模式或僅限蜂窩模式。
 此方法最多只能在每個任務中調用一次,並且僅在連接最初不可用時調用。 它永遠不會被調用后台會話,因為這些會話會忽略waitsForConnectivity。

 * Sent when a task cannot start the network loading process because the current
 * network connectivity is not available or sufficient for the task's request.
 *
 * This delegate will be called at most one time per task, and is only called if
 * the waitsForConnectivity property in the NSURLSessionConfiguration has been
 * set to YES.
 *
 * This delegate callback will never be called for background sessions, because
 * the waitForConnectivity property is ignored by those sessions.
 
 - (void)URLSession:(NSURLSession *)session taskIsWaitingForConnectivity:(NSURLSessionTask *)task
 API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
 
 告訴委托遠程服務器請求HTTP重定向。
 此方法僅適用於默認和臨時會話中的任務。 后台會話中的任務會自動遵循重定向。
 * An HTTP request is attempting to perform a redirection to a different
 * URL. You must invoke the completion routine to allow the
 * redirection, allow the redirection with a modified request, or
 * pass nil to the completionHandler to cause the body of the redirection
 * response to be delivered as the payload of this request. The default
 * is to follow redirections.
 *
 * For tasks in background sessions, redirections will always be followed and this method will not be called.
 
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
  willPerformHTTPRedirection:(NSHTTPURLResponse *)response
        newRequest:(NSURLRequest *)request
  completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler;

 
 響應來自遠程服務器的認證請求,從代理請求憑證。
 該方法處理任務級別的身份驗證挑戰。 NSURLSessionDelegate協議還提供了會話級別的身份驗證委托方法。所調用的方法取決於身份驗證挑戰的類型:
 對於會話級挑戰-NSURLAuthenticationMethodNTLM,NSURLAuthenticationMethodNegotiate,NSURLAuthenticationMethodClientCertificate或NSURLAuthenticationMethodServerTrust - NSURLSession對象調用會話委托的URLSession:didReceiveChallenge:completionHandler:方法。如果您的應用程序未提供會話委托方法,則NSURLSession對象會調用任務委托人的URLSession:task:didReceiveChallenge:completionHandler:方法來處理該挑戰。
 對於非會話級挑戰(所有其他挑戰),NSURLSession對象調用會話委托的URLSession:task:didReceiveChallenge:completionHandler:方法來處理挑戰。如果您的應用程序提供會話委托,並且您需要處理身份驗證,那么您必須在任務級別處理身份驗證,或者提供明確調用每會話處理程序的任務級別處理程序。會話委托的URLSession:didReceiveChallenge:completionHandler:方法不針對非會話級別的挑戰進行調用。
 
 * The task has received a request specific authentication challenge.
 * If this delegate is not implemented, the session specific authentication challenge
 * will *NOT* be called and the behavior will be the same as using the default handling
 * disposition.
 
 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
 didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;
 
 NOTE:注意區分上面的方法 這個是NSURLSessionDelegate代理方法
 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;

 當任務需要新的請求主體流發送到遠程服務器時,告訴委托。
 這種委托方法在兩種情況下被調用:
 1、如果使用uploadTaskWithStreamedRequest創建任務,則提供初始請求正文流:
 2、如果任務因身份驗證質詢或其他可恢復的服務器錯誤需要重新發送包含正文流的請求,則提供替換請求正文流。
 注:如果代碼使用文件URL或NSData對象提供請求主體,則不需要實現此功能。

 * Sent if a task requires a new, unopened body stream.  This may be
 * necessary when authentication has failed for any request that
 * involves a body stream.

 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
 needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler;

 
 定期通知代理向服務器發送主體內容的進度。(上傳進度)
 * Sent periodically to notify the delegate of upload progress.  This
 * information is also available as properties of the task.
 
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
    didSendBodyData:(int64_t)bytesSent
    totalBytesSent:(int64_t)totalBytesSent
    totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;

 告訴代理該會話完成了該任務的收集指標
 * Sent when complete statistics information has been collected for the task.
 
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
    didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
    API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

 告訴代理該任務完成傳輸數據
 * Sent as the last message related to a specific task.  Error may be
 * nil, which implies that no error occurred and this task is complete.
 
 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
    didCompleteWithError:(nullable NSError *)error;
*/

 

      下面我們一個一個的梳理這四個代理,它們和前面我們說的NSURLSessionTaskDelegate是有直接關系的,這個在看API的時候要隨時留意一點,特別是uploadTask的代理。

      我們再說說這個NSURLSessionDataDelegate,他就是遵守了上面的Task代理,以及后面的download都會是遵循了上面的Task代理的。

 

/*
 @protocol NSURLSessionDataDelegate <NSURLSessionTaskDelegate>
 @optional
 * This method will not be called for background upload tasks (which cannot be converted to download tasks).
 
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
 didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler;

 
 告訴代理數據任務已更改為下載任務。
 當委托的URLSession:dataTask:didReceiveResponse:completionHandler:方法決定將數據請求的處置更改為下載時,會話將調用此委托方法為你提供新的下載任務。 在此調用之后,會話委托不會收到與原始數據任務相關的其他委托方法調用。
 * Notification that a data task has become a download task.  No
 * future messages will be sent to the data task.
 
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
 didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask;

 告訴委托數據任務已更改為流任務
 當委托的URLSession:dataTask:didReceiveResponse:completionHandler:方法決定將處置從數據請求更改為流時,會話將調用此委托方法為你提供新的流任務。 在此調用之后,會話委托不會收到與原始數據任務相關的其他委托方法調用。
 
 對於pipelined的請求,流任務將只允許讀取,並且對象將立即發送委托消息URLSession:writeClosedForStreamTask :. 通過在其NSURLSessionConfiguration對象上設置HTTPShouldUsePipelining屬性,或通過在NSURLRequest對象上設置HTTPShouldUsePipelining屬性來為各個請求設置會話中的所有請求,可以禁用管道傳輸。

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
  didBecomeStreamTask:(NSURLSessionStreamTask *)streamTask;

 
 告訴代理該數據任務已經收到了一些預期的數據。
 由於NSData對象通常是由許多不同的數據對象拼湊而成的,因此盡可能使用NSData的enumerateByteRangesUsingBlock:方法遍歷數據,而不是使用bytes方法(將NSData對象平化為單個內存塊)。
 此委托方法可能被多次調用,並且每次調用僅提供自上次調用后收到的數據。 如果需要,該應用負責積累這些數據。
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data;

 詢問委托數據(或上傳)任務是否應將響應存儲在緩存中。
 會話在任務完成接收所有預期數據后調用此委托方法。如果未實現此方法,則默認行為是使用會話配置對象中指定的緩存策略。
 此方法的主要目的是防止特定URL的緩存或修改與URL響應關聯的userInfo字典。
 只有在處理請求的NSURLProtocol決定緩存響應時才調用此方法。通常,只有滿足以下所有條件時才會緩存響應:
 1、請求是針對HTTP或HTTPS URL(或你自己的支持緩存的自定義網絡協議)。
 2、請求成功(狀態碼在200-299范圍內)。
 3、提供的響應來自服務器,而不是緩存。
 4、會話配置的緩存策略允許緩存。
 5、提供的NSURLRequest對象的緩存策略(如果適用)允許緩存。
 6、服務器響應中的緩存相關頭(如果存在)允許緩存。
 7、響應大小足夠小,可以合理地放入緩存中。 (例如,如果您提供磁盤緩存,則響應不得超過磁盤緩存大小的5%。)
 注:如果委托實現此方法,則它必須調用completionHandler完成處理程序;否則,應用程序會泄漏內存。

 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
 willCacheResponse:(NSCachedURLResponse *)proposedResponse
 completionHandler:(void (^)(NSCachedURLResponse * _Nullable cachedResponse))completionHandler;
*/

  

      接下來就是下載代理:NSURLSessionDownloadDelegate,它的代理方法就三個,但確實是我們使用比較多的,我們也看看這三個代理方法的含義

      在我們的Demo中,我們寫后台下載的時候使用的也肯定是NSURLSessionDownloadDelegate,這個在Demo中有說:

 

/* 下載代理
 * Messages related to the operation of a task that writes data to a
 * file and notifies the delegate upon completion.
   @protocol NSURLSessionDownloadDelegate <NSURLSessionTaskDelegate>


 下載完成
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;

 
@optional
下載進度
Sent periodically to notify the delegate of download progress.
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
        didWriteData:(int64_t)bytesWritten
        totalBytesWritten:(int64_t)totalBytesWritten
        totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;

 
 下載任務已經恢復下載。
 
 參數:filrOffest:
 如果文件的緩存策略或上次修改日期阻止重新使用現有內容,則該值為零。否則,該值是一個整數,表示磁盤上不需要再次檢索的字節數。
 如果可恢復的下載任務被取消或失敗,可以請求resumeData對象,該對象將提供足夠的信息以重新開始下載。
 稍后,你可以調用downloadTaskWithResumeData:或downloadTaskWithResumeData:completionHandler:使用該數據。
 當你調用這些方法時,你會得到一個新的下載任務。只要恢復該任務,會話就會使用該新任務調用其委托的
 URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:方法,以指示恢復下載。
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
        didResumeAtOffset:(int64_t)fileOffset
        expectedTotalBytes:(int64_t)expectedTotalBytes;

*/

 

      接下來就是NSURLSessionStreamDelegate,這樣我們的幾個代理方法我們也就都說完了:

/*
 @protocol NSURLSessionStreamDelegate <NSURLSessionTaskDelegate>
 @optional

 告訴委托底層Socket的讀取面已經關閉。
 即使當前過程沒有讀取,也可以調用此方法。 此方法並不表示流達到end-of-file(EOF),
 從而不能讀取更多數據。
- (void)URLSession:(NSURLSession *)session readClosedForStreamTask:(NSURLSessionStreamTask *)streamTask;

 上面讀這個就是寫的說明
 告訴委托底層套接字的寫入端已關閉。
 即使當前過程沒有寫入,也可以調用此方法。
- (void)URLSession:(NSURLSession *)session writeClosedForStreamTask:(NSURLSessionStreamTask *)streamTask;

 
 告訴委托流已經檢測到通往主機更好的路由  下面的例子是WiFi可用了
- (void)URLSession:(NSURLSession *)session betterRouteDiscoveredForStreamTask:(NSURLSessionStreamTask *)streamTask;

 告訴委托,流任務已完成,由於流任務調用captureStreams方法。
 此委托方法僅在流任務的所有入隊讀取和寫入操作完成后才會調用。
- (void)URLSession:(NSURLSession *)session streamTask:(NSURLSessionStreamTask *)streamTask
        didBecomeInputStream:(NSInputStream *)inputStream
        outputStream:(NSOutputStream *)outputStream;
*/

 

 

NSURLSessionTaskTransactionMetrics和NSURLSessionTaskMetrics


 

 

     NSURLSessionTaskTransactionMetrics 主要使用這個API獲取每個階段的網絡請求時常,等數據,來分析對應的網絡請求。

/*
   This class defines the performance(性能) metrics collected for a request/response transaction     during the task execution.執行
主要使用這個API獲取每個階段的網絡請求時常,等數據,來分析對應的網絡請求。
   API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0))
   @interface NSURLSessionTaskTransactionMetrics : NSObject
*/

/*
    presents 禮物復數  表現
    Represents 代表 the transaction request.
    @property (copy, readonly) NSURLRequest *request;
 */

/*
   如果發生錯誤並且沒有生成響應,則可以為nil
   @property (nullable, copy, readonly) NSURLResponse *response;
 */

/*
 * fetchStartDate returns the time when the user agent started fetching the resource, whether or not the resource was retrieved from the server or local resources.
 * 用戶代理開始獲取資源的時間,無論是否從服務器或本地資源中檢索資源。
 * The following metrics will be set to nil, if a persistent connection was used or the resource was retrieved from local resources:

 *   domainLookupStartDate
 *   domainLookupEndDate
 *   connectStartDate
 *   connectEndDate
 *   secureConnectionStartDate
 *   secureConnectionEndDate
     @property (nullable, copy, readonly) NSDate *fetchStartDate;
 */

/* 用戶代理啟動資源名稱查找之前的時間。
    domainLookupStartDate returns the time immediately before the user agent started the name lookup for the resource.
   @property (nullable, copy, readonly) NSDate *domainLookupStartDate;
 */

/* 名稱查詢完成后的時間。
 * domainLookupEndDate returns the time after the name lookup was completed.
   @property (nullable, copy, readonly) NSDate *domainLookupEndDate;
 */

/* 用戶代理開始建立到服務器的連接之前的時間。
    connectStartDate is the time immediately before the user agent started establishing the connection to the server.
  
    For example, this would correspond to the time immediately before the user agent started trying to establish the TCP connection.
   @property (nullable, copy, readonly) NSDate *connectStartDate;
*/

/* 如果使用加密連接,則secureConnectionStartDate是用戶代理剛剛開始安全握手以保護當前連接之前的時間。如果未使用加密連接,則此屬性設置為零。
   If an encrypted 加密 connection was used, secureConnectionStartDate is the time immediately before the user agent 代理 started the
   security 安全 handshake to secure the current connection.
   For example, this would correspond to the time immediately before the user agent started the TLS handshake.
   If an encrypted connection was not used, this attribute is set to nil.
   @property (nullable, copy, readonly) NSDate *secureConnectionStartDate;
 */

/* 如果使用加密連接,則secureConnectionEndDate是安全握手完成后的時間。如果未使用加密連接,則此屬性設置為零
 * If an encrypted connection was used, secureConnectionEndDate is the time immediately after the security handshake completed.
 *
 * If an encrypted connection was not used, this attribute is set to nil.
   @property (nullable, copy, readonly) NSDate *secureConnectionEndDate;
 */

/* 用戶代理完成與服務器建立連接后的時間,包括完成與安全相關的握手和其他握手
 * connectEndDate is the time immediately after the user agent finished establishing the connection to the server, including completion of security-related and other handshakes.
   @property (nullable, copy, readonly) NSDate *connectEndDate;
 */

/* 用戶代理開始請求源之前的時間,無論是從服務器還是從本地資源中檢索資源。
    requestStartDate is the time immediately before the user agent started requesting the source, regardless of whether the resource was retrieved from the server or local resources.
    For example, this would correspond to the time immediately before the user agent sent an HTTP GET request.
   @property (nullable, copy, readonly) NSDate *requestStartDate;
 */

/* 用戶代理完成請求源后的時間,無論資源是從服務器還是從本地資源中檢索
    requestEndDate is the time immediately after the user agent finished requesting the source, regardless of whether the resource was retrieved from the server or local resources.
    For example, this would correspond to the time immediately after the user agent finished sending the last byte of the request.
   @property (nullable, copy, readonly) NSDate *requestEndDate;
 */

/* 用戶代理剛收到服務器或本地資源響應的第一個字節后的時間。
   responseStartDate is the time immediately after the user agent received the first byte of the response from the server or from local resources.
  
   For example, this would correspond to the time immediately after the user agent received the first byte of an HTTP response.
   @property (nullable, copy, readonly) NSDate *responseStartDate;
 */

/* 用戶代理收到資源的最后一個字節后的時間。
   responseEndDate is the time immediately after the user agent received the last byte of the resource.
   @property (nullable, copy, readonly) NSDate *responseEndDate;
 */

/* 用於獲取資源的網絡協議,由ALPN協議ID標識序列[RFC7301]標識。如果配置了代理並建立了隧道連接,則此屬性將返回隧道協議的值。
 * The network protocol used to fetch the resource, as identified by the ALPN Protocol ID Identification Sequence [RFC7301].
 * E.g., h2, http/1.1, spdy/3.1.
 *
 * When a proxy is configured AND a tunnel connection is established, then this attribute returns the value for the tunneled protocol.
 *
 * For example:
 * If no proxy were used, and HTTP/2 was negotiated, then h2 would be returned.
 * If HTTP/1.1 were used to the proxy, and the tunneled connection was HTTP/2, then h2 would be returned.
 * If HTTP/1.1 were used to the proxy, and there were no tunnel, then http/1.1 would be returned.
 
   @property (nullable, copy, readonly) NSString *networkProtocolName;
 *
 */

/* 如果使用代理連接來獲取資源,則此屬性設置為YES。
 * This property is set to YES if a proxy connection was used to fetch the resource.
 
   @property (assign, readonly, getter=isProxyConnection) BOOL proxyConnection;
 */

/* 如果使用持續連接來獲取資源,則此屬性設置為YES
    This property is set to YES if a persistent connection was used to fetch the resource.
    @property (assign, readonly, getter=isReusedConnection) BOOL reusedConnection;
 */

/* 指示資源是否已從本地緩存中加載,推送或檢索。
    Indicates whether the resource was loaded, pushed or retrieved from the local cache.
    @property (assign, readonly) NSURLSessionTaskMetricsResourceFetchType resourceFetchType;
 */

/* API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0))
    @interface NSURLSessionTaskMetrics : NSObject
 */

/* 包含為在任務執行期間創建的每個請求/響應事務收集的度量標准。
    transactionMetrics array contains the metrics collected for every request/response transaction created during the task execution.
    @property (copy, readonly) NSArray<NSURLSessionTaskTransactionMetrics *>       transactionMetrics;
 */

/* 從任務創建時間到任務完成時間的時間間隔。
   @property (copy, readonly) NSDateInterval *taskInterval;

 */

/* 記錄的重定向的數量。
    redirectCount is the number of redirects that were recorded.
    @property (assign, readonly) NSUInteger redirectCount;
 */

 

最后小 demo


 

      最后是一個斷點下載和后台下載的一個Demo,具體的Demo代碼我就不再具體的講解了,因為在Demo中加了很多的注釋。

      由於這個Demo是寫在以前的關於AVFoundation的Demo里面,具體的Demo是在NSURLSession文件中。

      還有前面關於API的注釋也是在寫URLSessionManager.h文件中,需要的可以翻翻去看看,關於NSURLSession暫時就先說這么多,有問題可以加我QQ 1872684219  

      Demo 點擊這里下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM