版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2018.03.16 |
前言
我們做APP發(fā)起網(wǎng)絡(luò)請求,一般都是使用框架,這些框架的底層也都是蘋果的API,接下來幾篇就一起來看一下和網(wǎng)絡(luò)有關(guān)的幾個類。感興趣的可以看上面幾篇文章。
1. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類 (一) —— NSURLSession
2. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(二) —— NSURLRequest和NSMutableURLRequest
3. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(三) —— NSURLConnection
4. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(四) —— NSURLSession和NSURLConnection的區(qū)別
5. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(五) —— 關(guān)于NSURL加載系統(tǒng)(一)
6. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(六) —— 使用NSURLSession(二)
7. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(七) —— URL數(shù)據(jù)的編碼和解碼(三)
8. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(八) —— 處理重定向和其他請求更改(四)
9. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(九) —— 身份驗證挑戰(zhàn)和TLS鏈驗證(五)
10. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十) —— 理解獲取緩存(六)
11. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十一) —— Cookies和自定義協(xié)議(七)
12. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十二) —— URL Session的生命周期(八)
13. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十三) —— NSURLResponse(一)
14. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十四) —— NSHTTPCookie(一)
15. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十五) —— NSHTTPCookieStorage(一)
16. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十六) —— NSURLCache(一)
17. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十七) —— NSCachedURLResponse(一)
回顧
上一篇講述了NSCachedURLResponse這個類的詳細信息以及一些注意要點,下面這篇我們就主要看一下NSURLAuthenticationChallenge。
Overview
先看一個該類的基本信息。

大多數(shù)應用程序本身不會創(chuàng)建認證挑戰(zhàn)。但是,在添加對自定義網(wǎng)絡(luò)協(xié)議的支持時,您可能需要創(chuàng)建身份驗證質(zhì)詢對象,作為自定義NSURLProtocol子類的一部分。
相反,您的應用在各種NSURLSession,NSURLConnection和NSURLDownload代理方法(如URLSession:task:didReceiveChallenge:completionHandler:)中接收到認證挑戰(zhàn)。這些對象提供了在決定如何處理服務器的身份驗證請求時所需的信息。該認證挑戰(zhàn)的核心是一個保護空間,該空間定義所請求的認證類型,主機和端口號,網(wǎng)絡(luò)協(xié)議和(在適用的情況下)認證領(lǐng)域(一組相關(guān)服務器上的相關(guān)URL一組憑據(jù))。
您的應用通過提供NSURLCredential對象來響應身份驗證挑戰(zhàn)。細節(jié)取決于您使用的API和挑戰(zhàn)類型。
在較高層次上,如果您向服務器或代理提供用戶憑據(jù),proposedCredential方法將提供一個憑證,該憑證與保護空間中指定的條件相匹配,從處理請求的NSURLCredentialStorage類中檢索(假設(shè)存在此憑據(jù)) 。
如果previousFailureCount方法返回0并且提出的憑證存在,提出的憑證尚未嘗試,這意味著您應該嘗試。如果它返回非零結(jié)果,那么服務器已經(jīng)拒絕了建議的憑證,并且您應該使用該憑證來填充密碼或證書選擇器對話框,然后提供新的憑證。您可以通過調(diào)用credentialWithUser:password:persistence:方法創(chuàng)建基于密碼的憑證,或使用 credentialWithIdentity:certificates:persistence:創(chuàng)建基于證書的憑證。
如果身份驗證的保護空間使用NSURLAuthenticationMethodServerTrust身份驗證方法,則請求會要求您驗證服務器的真實性。在這種情況下,proposedCredential方法提供基于服務器作為其初始TLS握手的一部分提供的證書的憑證。大多數(shù)應用程序應基于服務器信任保護空間請求對身份驗證挑戰(zhàn)的默認處理,但如果您需要覆蓋默認的TLS驗證行為,則可以按照Overriding TLS Chain Validation Correctly中所述執(zhí)行此操作。
有關(guān)URL會話如何處理不同類型的身份驗證挑戰(zhàn)的更多信息,請參閱NSURLSession和URL Session Programming Guide。
Topics
1. Creating an authentication challenge instance - 創(chuàng)建一個認證挑戰(zhàn)實例
-
- initWithAuthenticationChallenge:sender:
- 返回初始化的
NSURLAuthenticationChallenge對象,從challenge中復制屬性并將身份驗證發(fā)件人設(shè)置為sender。
- 返回初始化的
-
- initWithProtectionSpace:proposedCredential:previousFailureCount:failureResponse:error:sender:
- 為指定的保護空間,憑證,失敗計數(shù),服務器響應,錯誤和sender返回初始化的
NSURLAuthenticationChallenge對象。
- 為指定的保護空間,憑證,失敗計數(shù),服務器響應,錯誤和sender返回初始化的
2. Getting authentication challenge properties - 獲取認證挑戰(zhàn)屬性
-
error
- 代表最后授權(quán)失敗的error對象。
-
failureResponse
- 代表最后授權(quán)失敗的URL響應對象。
-
previousFailureCount
- 接收方失敗的認證嘗試次數(shù)。
-
proposedCredential
- 針對此挑戰(zhàn)的建議憑證。
-
protectionSpace
- 接受者的保護空間。
-
sender
- 接受者的sender。
-
NSURLAuthenticationChallengeSender
-
NSURLAuthenticationChallengeSender協(xié)議表示認證挑戰(zhàn)的發(fā)送者必須實現(xiàn)的接口。
-
API
1. NSURLAuthenticationChallenge類
@class NSURLAuthenticationChallengeInternal;
/*!
@class NSURLAuthenticationChallenge
@discussion This class represents an authentication challenge. It
provides all the information about the challenge, and has a method
to indicate when it's done.
*/
@interface NSURLAuthenticationChallenge : NSObject <NSSecureCoding>
{
@private
NSURLAuthenticationChallengeInternal *_internal;
}
/*!
@method initWithProtectionSpace:proposedCredential:previousFailureCount:failureResponse:error:
@abstract Initialize an authentication challenge
@param space The NSURLProtectionSpace to use
@param credential The proposed NSURLCredential for this challenge, or nil
@param previousFailureCount A count of previous failures attempting access.
@param response The NSURLResponse for the authentication failure, if applicable, else nil
@param error The NSError for the authentication failure, if applicable, else nil
@result An authentication challenge initialized with the specified parameters
*/
// 實例化一個驗證挑戰(zhàn)對象
- (instancetype)initWithProtectionSpace:(NSURLProtectionSpace *)space proposedCredential:(nullable NSURLCredential *)credential previousFailureCount:(NSInteger)previousFailureCount failureResponse:(nullable NSURLResponse *)response error:(nullable NSError *)error sender:(id<NSURLAuthenticationChallengeSender>)sender;
/*!
@method initWithAuthenticationChallenge:
@abstract Initialize an authentication challenge copying all parameters from another one.
@result A new challenge initialized with the parameters from the passed in challenge
@discussion This initializer may be useful to subclassers that want to proxy
one type of authentication challenge to look like another type.
*/
// 初始化認證挑戰(zhàn),從另一個參數(shù)復制所有參數(shù)、
// 此初始化程序可能對希望代理一種類型的認證挑戰(zhàn)以使其看起來像另一種類型的子級別者有用
- (instancetype)initWithAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge sender:(id<NSURLAuthenticationChallengeSender>)sender;
/*!
@abstract Get a description of the protection space that requires authentication
@result The protection space that needs authentication
*/
// 獲取一個需要認證的保護空間的描述
@property (readonly, copy) NSURLProtectionSpace *protectionSpace;
/*!
@abstract Get the proposed credential for this challenge
@result The proposed credential
@discussion proposedCredential may be nil, if there is no default
credential to use for this challenge (either stored or in the
URL). If the credential is not nil and returns YES for
hasPassword, this means the NSURLConnection thinks the credential
is ready to use as-is. If it returns NO for hasPassword, then the
credential is not ready to use as-is, but provides a default
username the client could use when prompting.
*/
// 獲取此挑戰(zhàn)的建議憑據(jù)
// 如果沒有默認憑據(jù)用于此挑戰(zhàn)(存儲或在URL中),則建議的認證可能為nil。
// 如果憑證不為nil并且hasPassword返回YES,這意味著NSURLConnection認為憑證可以按原樣使用。
// 如果它返回NO作為hasPassword,那么憑證尚未準備就緒,但提供了一個默認用戶名,客戶端可以在提示時使用該用戶名。
@property (nullable, readonly, copy) NSURLCredential *proposedCredential;
/*!
@abstract Get count of previous failed authentication attempts
@result The count of previous failures
*/
// 以前嘗試驗證失敗的次數(shù)
@property (readonly) NSInteger previousFailureCount;
/*!
@abstract Get the response representing authentication failure.
@result The failure response or nil
@discussion If there was a previous authentication failure, and
this protocol uses responses to indicate authentication failure,
then this method will return the response. Otherwise it will
return nil.
*/
// 代表驗證失敗的響應
@property (nullable, readonly, copy) NSURLResponse *failureResponse;
/*!
@abstract Get the error representing authentication failure.
@discussion If there was a previous authentication failure, and
this protocol uses errors to indicate authentication failure,
then this method will return the error. Otherwise it will
return nil.
*/
// 獲取表示認證失敗的錯誤。
// 如果以前的認證失敗,并且此協(xié)議使用錯誤來指示認證失敗,則此方法將返回錯誤。 否則它將返回nil。
@property (nullable, readonly, copy) NSError *error;
/*!
@abstract Get the sender of this challenge
@result The sender of the challenge
@discussion The sender is the object you should reply to when done processing the challenge.
*/
// 獲取該挑戰(zhàn)的sender
// sender就是當你處理完挑戰(zhàn)應該回復的對象。
@property (nullable, readonly, retain) id<NSURLAuthenticationChallengeSender> sender;
@end
2. NSURLAuthenticationChallengeSender協(xié)議
/*!
@protocol NSURLAuthenticationChallengeSender
@discussion This protocol represents the sender of an
authentication challenge. It has methods to provide a credential,
to continue without any credential, getting whatever failure
result would happen in that case, cancel a challenge, perform the default
action as defined by the system, or reject the currently supplied protection-space
in the challenge.
*/
// 此協(xié)議代表身份驗證質(zhì)詢的發(fā)起人。 它提供了一種方法來提供證書,
// 在沒有任何證書的情況下繼續(xù)獲取在這種情況下會發(fā)生的任何失敗結(jié)果,取消挑戰(zhàn),
// 執(zhí)行系統(tǒng)定義的默認操作,或拒絕挑戰(zhàn)中當前提供的保護空間。
@protocol NSURLAuthenticationChallengeSender <NSObject>
/*!
@method useCredential:forAuthenticationChallenge:
*/
- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
/*!
@method continueWithoutCredentialForAuthenticationChallenge:
*/
- (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
/*!
@method cancelAuthenticationChallenge:
*/
- (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
@optional
/*!
@method performDefaultHandlingForAuthenticationChallenge:
*/
- (void)performDefaultHandlingForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
/*!
@method rejectProtectionSpaceAndContinueWithChallenge:
*/
- (void)rejectProtectionSpaceAndContinueWithChallenge:(NSURLAuthenticationChallenge *)challenge;
@end
后記
本篇主要講述
NSURLAuthenticationChallenge這個類的詳細信息。
