版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.02.25 |
前言
我們做APP邻眷,文字和圖片是絕對不可缺少的元素剔交,特別是圖片一般存儲在圖床里面岖常,一般公司可以委托第三方保存,NB的公司也可以自己存儲圖片,ios有很多圖片加載的第三方框架,其中最優(yōu)秀的莫過于SDWebImage冯乘,它幾乎可以滿足你所有的需求,用了好幾年這個框架晒夹,今天想總結一下惋戏。感興趣的可以看其他幾篇。
1. SDWebImage探究(一)
2. SDWebImage探究(二)
3. SDWebImage探究(三)
4. SDWebImage探究(四)
5. SDWebImage探究(五)
6. SDWebImage探究(六) —— 圖片類型判斷深入研究
7. SDWebImage探究(七) —— 深入研究圖片下載流程(一)之有關option的位移枚舉的說明
8. SDWebImage探究(八) —— 深入研究圖片下載流程(二)之開始下載并返回下載結果的總的方法
9. SDWebImage探究(九) —— 深入研究圖片下載流程(三)之下載之前的緩存查詢操作
10. SDWebImage探究(十) —— 深入研究圖片下載流程(四)之查詢緩存后的block回調處理
11. SDWebImage探究(十一) —— 深入研究圖片下載流程(五)之SDWebImageDownloadToken和操作對象的生成和返回
12. SDWebImage探究(十二) —— 深入研究圖片下載流程(六)之下載器到具體下載操作的代理分發(fā)實現(xiàn)
13. SDWebImage探究(十三) —— 深入研究圖片下載流程(七)之NSURLSession中幾個代理的基本用法和關系
14. SDWebImage探究(十四) —— 深入研究圖片下載流程(八)之下載完成代理方法的調用
SDWebImageDownloaderOperation中身份驗證質詢代理方法
該代理方法主要用于:該任務已收到請求特定的身份驗證質詢
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler;
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__block NSURLCredential *credential = nil;
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if (!(self.options & SDWebImageDownloaderAllowInvalidSSLCertificates)) {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
} else {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
disposition = NSURLSessionAuthChallengeUseCredential;
}
} else {
if (challenge.previousFailureCount == 0) {
if (self.credential) {
credential = self.credential;
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
}
if (completionHandler) {
completionHandler(disposition, credential);
}
}
該代理方法主要做一下幾個工作:
- 構造參數(shù)
NSURLSessionAuthChallengeDisposition disposition
和NSURLCredential *credential
。 - 回調block
completionHandler
舔亭,將上面獲取的參數(shù)作為輸入?yún)?shù)傳遞completionHandler(disposition, credential)
蟀俊。
disposition和credential參數(shù)的構建
1. NSURLSessionAuthChallengeDisposition枚舉
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__block NSURLCredential *credential = nil;
首先我們要看一下NSURLSessionAuthChallengeDisposition
這個枚舉订雾。首先看一下API然后在看其用法。
typedef NS_ENUM(NSInteger, NSURLSessionAuthChallengeDisposition) {
NSURLSessionAuthChallengeUseCredential = 0,
/* Use the specified credential, which may be nil */
//使用指定的credential洼哎,可能為nil
NSURLSessionAuthChallengePerformDefaultHandling = 1,
/* Default handling for the challenge - as if this delegate were not implemented; the credential parameter is ignored. */
//挑戰(zhàn)challenge的默認處理 - 就好像這個代理沒有實現(xiàn)噩峦,憑證credential參數(shù)被忽略
NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2,
/* The entire request will be canceled; the credential parameter is ignored. */
// 整個的請求將被取消,忽略credential參數(shù)族淮。
NSURLSessionAuthChallengeRejectProtectionSpace = 3,
/* This challenge is rejected and the next authentication protection space should be tried; the credential parameter is ignored. */
//這個挑戰(zhàn)被拒絕祝辣,并且應該嘗試下一個認證保護空間蝙斜; 憑證credential參數(shù)被忽略
} NS_ENUM_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0);
這里其實就是用來進行身份驗證的白翻,這個和HTTP還有HTTPS有關滤馍,大家都知道HTTPS更安全底循,很多國際大公司都提倡使用HTTPS。下面兩張圖可以很明顯的說明二者的區(qū)別阁苞,圖片來源網(wǎng)絡那槽,后面已經增加引用參考說明等舔。
從上面可以看見甚牲,由于增加受保護空間以及公鑰的加密,信息在相對更安全的路徑進行傳輸非驮。
下面回到SDWebImage中雏赦,處理方式disposition首先默認取值NSURLSessionAuthChallengePerformDefaultHandling
喉誊,這個的意思就是使用默認方式進行處理,其實這就相當于一個變量的初始化栋盹。
2. if條件判斷
if條件判斷主要對應下面這一小段代碼例获。
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if (!(self.options & SDWebImageDownloaderAllowInvalidSSLCertificates)) {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
else {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
disposition = NSURLSessionAuthChallengeUseCredential;
}
}
首先看一下判斷條件[challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]
/*!
@const NSURLAuthenticationMethodServerTrust
@abstract SecTrustRef validation required. Applies to any protocol.
*/
FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodServerTrust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));
這個判斷條件的作用就是判斷服務器返回的證書是否是服務器信任的榨汤。
如果是信任的收壕,接著進行判斷轨蛤,如果options不是SDWebImageDownloaderAllowInvalidSSLCertificates
。
/**
* Enable to allow untrusted SSL certificates.
* Useful for testing purposes. Use with caution in production.
*/
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
也就是不允許無效的證書圃验,那么disposition = NSURLSessionAuthChallengePerformDefaultHandling
澳窑,確定處理方式這個參數(shù)的值摊聋。如果options是SDWebImageDownloaderAllowInvalidSSLCertificates
呢栈暇,也就是允許無效的整數(shù),那么按照下面進行處理悲立。
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
disposition = NSURLSessionAuthChallengeUseCredential;
實例化新的NSURLCredential證書,并且設置dispose為使用指定的證書這個枚舉值NSURLSessionAuthChallengeUseCredential
薪夕,在這一點上都是類似的原献,AFN有一部分也是這么做的。
3. else條件判斷
如果服務器返回的證書不是服務器信任的写隶。那么慕趴,按照下面這小段邏輯進行處理鄙陡。
else {
if (challenge.previousFailureCount == 0) {
if (self.credential) {
credential = self.credential;
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
}
這里首先判斷以前的質詢次數(shù)趁矾,對應previousFailureCount
這個屬性。
/*!
@abstract Get count of previous failed authentication attempts
@result The count of previous failures
*/
@property (readonly) NSInteger previousFailureCount;
如果質詢失敗次數(shù)為0详拙,接著進行判斷饶辙,如果有證書牌柄,那么disposition = NSURLSessionAuthChallengeUseCredential;
珊佣,如果沒有證書披粟,那么disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
,也就是整個的請求將被取消惑艇,忽略credential參數(shù)滨巴。
如果質詢失敗次數(shù)不為0,那么disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
泰偿,作用同上耗跛。
completionHandler回調
上面的參數(shù)構建完畢后攒发,緊接著就是completionHandler
這個block的回調「崂回調就是這兩個參數(shù)偶妖,處理方式和證書completionHandler(disposition, credential);
參考文章
1. iOS - HTTPS
2. 打造安全的App餐屎!iOS安全系列之 HTTPS
后記
本篇主要解析的就是質詢時候的代理處理方法,構造disposition和credential兩個參數(shù)屿聋,并且回調作為輸入?yún)?shù)傳過去藏鹊。