iOS-----AFNetwoking

概述


從上圖可以看出,AFNetwoking框架是一個比較簡潔的框架,主要分了幾個部分:

1.->網絡通信模塊:NSURLSession

2->網絡狀態監聽模塊:Reachability

3->網絡通信安全模塊:Security

4->網絡通信序列化模塊:Serialization

5->對UIKit框架擴展部分:UIKit(以Catagory形式添加特性)

在這幾個模塊中,AFNetwoking的核心模塊是通信模塊,在通信模塊有兩個類,AFHttpSessionManager和AFURLSessionManager,其中前者繼承于后者,是對于HTTP的專一化封裝處理。AFNetworking 3.0其實只是對NSURLSession做了封裝處理

/*使用NSURLSession和使用AFNetworking做網絡請求在實現過程中有什么區別*/


*使用區別

1.使用進行網絡請求

NSDictionary *dict =@{@"MENU_VERSION":@"",

@"INCORP_NO":@"000",

@"REQ_TIME":[self getParrentTime],

@"CLIENT_TYPE":@"1"

};

NSString *strData =[dict JSONRepresentation];

NSMutableURLRequest* mutableRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://www.com.cn/direct/cust/MenuQuery.do"]];

NSString *httpContentType = [NSString stringWithFormat:@"%@;%@",@"application/json",@"charset=UTF-8"];

[mutableRequest setValue:httpContentType forHTTPHeaderField:@"Content-Type"];

NSMutableData* postData = [[NSMutableData alloc]init];

[postData appendData:[strData dataUsingEncoding:NSUTF8StringEncoding]];

mutableRequest.HTTPMethod = @"POST";

mutableRequest.HTTPBody = postData;

NSURLSession* session = [NSURLSession sharedSession];

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSLog(@"%@",data);

NSLog(@"%@",response);

NSLog(@"%@",error);

NSString* dataStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",dataStr);

}];

[dataTask resume];

2.使用AFHTTP

GET請求

AFHTTPSessionManager* sessionManager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://www.com.cn"]];

[sessionManager GET:@"direct/cust/MenuQuery.do" parameters:strData progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {

NSLog(@"%@",responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"%@",error);

}];

//POST請求

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

NSMutableDictionary *parameters = [@{@"1":@"2",@"3":@"4"} mutableCopy];

[manager POST:@"http://www.com.cn" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

}];

AFNetworking下載

- (void)downLoad{

//1.創建管理者對象

AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];

//2.確定請求的URL地址

NSURL* url =[[NSURL alloc]initWithString:@"hrrps:www.com.cn"];

//3.創建請求對象

NSURLRequest* request = [NSURLRequest requestWithURL:url];

//4.下載任務

NSURLSessionDownloadTask* task = [sessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

//打印下載進度

} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

//下載地址----targetPath

//設置下載路徑,通過沙盒獲取緩存地址,最后返回NSURL對象

NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];

return [NSURL fileURLWithPath:filePath];

} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

//下載完成調用的方法

}];

//開始啟動任務

[task resume];

}

//第一種上傳方法------通過工程中的文件進行上傳

- (void)upload1{? ? ? ?

?//1.創建管理者對象??

? AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];? ?

?//2.上傳文件? ? NSDictionary* dict = @{@"key":@"value"};

//參數? ? [sessionManager POST:@"https://www.com.cn" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {

//上傳文件參數

UIImage *iamge = [UIImage imageNamed:@"123.png"];

NSData *data = UIImagePNGRepresentation(iamge);

//這個就是參數

[formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"];

} progress:^(NSProgress * _Nonnull uploadProgress) {

//進度

} success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {

//請求成功

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

//請求失敗

}];

}

//第二種上傳方法------通過URL來獲取路徑,進入沙盒或者系統相冊等

- (void)upload2{? ? ? ?

?//1.創建管理者對象? ??

AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];??

? //2.上傳文件? ? NSDictionary* dict = @{@"key":@"value"};

//參數? ? [sessionManager POST:@"https://www.com.cn" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {

//上傳文件參數

[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件地址"] name:@"file" fileName:@"1234.png" mimeType:@"application/octet-stream" error:nil];

} progress:^(NSProgress * _Nonnull uploadProgress) {

//進度

} success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {

//請求成功

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

//請求失敗

}];

}

//對當前網絡進行監聽

- (void)AFNetworkStatus{

//1.創建網絡監測者

AFNetworkReachabilityManager* manager = [AFNetworkReachabilityManager sharedManager];

/*

網絡的四種狀態:

AFNetworkReachabilityStatusUnknown? ? ? ? ? = -1,? ? ? 未知

AFNetworkReachabilityStatusNotReachable? ? = 0,? ? ? 無網絡

AFNetworkReachabilityStatusReachableViaWWAN = 1,? ? ? 蜂窩數據網絡

AFNetworkReachabilityStatusReachableViaWiFi = 2,? ? ? WiFi

*/

[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

//這里是監測到網絡改變的block? 可以寫成switch方便

//在里面可以隨便寫事件

switch (status) {

case AFNetworkReachabilityStatusUnknown:

NSLog(@"未知網絡狀態");

break;

case AFNetworkReachabilityStatusNotReachable:

NSLog(@"無網絡");

break;

case AFNetworkReachabilityStatusReachableViaWWAN:

NSLog(@"蜂窩數據網");

break;

case AFNetworkReachabilityStatusReachableViaWiFi:

NSLog(@"WiFi網絡");

break;

default:

break;

}

}];

}

可以發現使用AFHTTPSessionManager進行網絡請求大致分為了兩步:

1->創建一個AFHTTPSessionManager對象

2->使用這個對象調用含有block的請求方法

從調用上來看,AFNetworking的請求會更加易讀和編寫

從對AFNetworking的源碼分析可以發現,AFNetworking的內部實現到棧低仍然是操作了原生的NSURLSession,從根本上只是對原生的NSURLSession做了封裝操作,封裝了一些序列化、通信安全等策略,提供簡潔的API,方便用戶編碼。

注意的是,AFNetworking 默認接收 json 格式的響應(因為這是在 iOS 平臺上的框架,一般不需要 text/html),如果想要返回 html,需要設置 acceptableContentTypes


引入 AFSecurityPolicy 保證請求的安全

AFSecurityPolicy 是 AFNetworking 用來保證 HTTP 請求安全的類,它被 AFURLSessionManager 持有,如果你在 AFURLSessionManager 的實現文件中搜索 self.securityPolicy,你只會得到三條結果:

初始化 self.securityPolicy = [AFSecurityPolicy defaultPolicy]

收到連接層的驗證請求時

任務接收到驗證請求時

在 API 調用上,后兩者都調用了 - [AFSecurityPolicy evaluateServerTrust:forDomain:] 方法來判斷當前服務器是否被信任,實現代碼

- (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 (self.taskDidReceiveAuthenticationChallenge) {

disposition = self.taskDidReceiveAuthenticationChallenge(session, task, challenge, &credential);

} else {

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {

disposition = NSURLSessionAuthChallengeUseCredential;

credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

} else {

disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;

}

} else {

disposition = NSURLSessionAuthChallengePerformDefaultHandling;

}

}

if (completionHandler) {

completionHandler(disposition, credential);

}

}

如果沒有傳入 taskDidReceiveAuthenticationChallenge block,只有在上述方法返回 YES 時,才會獲得認證憑證 credential。

NSURLAuthenticationChallenge 表示一個認證的挑戰,提供了關于這次認證的全部信息。它有一個非常重要的屬性 protectionSpace,這里保存了需要認證的保護空間, 每一個 NSURLProtectionSpace 對象都保存了主機地址,端口和認證方法等重要信息。

在上面的方法中,如果保護空間中的認證方法為 NSURLAuthenticationMethodServerTrust,那么就會使用在上一小節中提到的方法

- [AFSecurityPolicy evaluateServerTrust:forDomain:] 對保護空間中的 serverTrust 以及域名 host 進行認證

根據認證的結果,會在 completionHandler 中傳入不同的 disposition 和 credential 參數。

自 iOS9 發布之后,由于新特性 App Transport Security 的引入,在默認行為下是不能發送 HTTP 請求的。很多網站都在轉用 HTTPS,而 AFNetworking 中的 AFSecurityPolicy 就是為了阻止中間人攻擊,以及其它漏洞的工具。

AFSecurityPolicy 主要作用就是驗證 HTTPS 請求的證書是否有效,如果 app 中有一些敏感信息或者涉及交易信息,一定要使用 HTTPS 來保證交易或者用戶信息的安全。

使用 AFSecurityPolicy 時,總共有三種驗證服務器是否被信任的方式:

typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {

AFSSLPinningModeNone,

AFSSLPinningModePublicKey,

AFSSLPinningModeCertificate,

};

AFSSLPinningModeNone 是默認的認證方式,只會在系統的信任的證書列表中對服務端返回的證書進行驗證

AFSSLPinningModeCertificate 需要客戶端預先保存服務端的證書

AFSSLPinningModeCertificate 也需要預先保存服務端發送的證書,但是這里只會驗證證書中的公鑰是否正確

2> allowInvalidCertificates

allowInvalidCertificates 定義了客戶端是否信任非法證書。一般來說,每個版本的iOS設備中,都會包含一些既有的CA根證書。如果接收到的證書是iOS信任的CA根證書簽名的,那么則為合法證書;否則則為“非法”證書。

allowInvalidCertificates 就是用來確認是否信任這樣的證書的。當然,我們也可以給iOS加入新的信任的CA證書。iOS已有的CA根證書,可以在這里了解到:https://support.apple.com/en-us/HT204132

3> pinnedCertificates

pinnedCertificates 就是用來校驗服務器返回證書的證書。通常都保存在mainBundle 下。通常默認情況下,AFNetworking會自動尋找在mainBundle的根目錄下所有的.cer文件并保存在pinnedCertificates數組里,以校驗服務器返回的證書。

4> validatesDomainName

validatesDomainName 是指是否校驗在證書中的domain這一個字段。每個證書都會包含一個DomainName, 它可以是一個IP地址,一個域名或者一端帶有通配符的域名。如*.google.com, www.google.com 都可以成為這個證書的DomainName。設置validatesDomainName=YES將嚴格地保證其安全性。

5> validatesCertificateChain

validatesCertificateChain 指的是是否校驗其證書鏈。

通常來講,一個CA證書頒發機構有很多個子機構,用來簽發不同用途的子證書,然后這些子證書又再用來簽發相應的證書。只有證書鏈上的證書都正確,CertificateChain才算驗證完成。

做好以上工作后,您應該就可以正常訪問您自己的https服務器了。如果還是有問題請檢查:

(1)HTTPS服務器的正確配置。一般來說,可以使用瀏覽器打開相同頁面來查看瀏覽器上的小鎖是否正常。

(2)是否https.cer正確打包進了項目中。

總結:

AFNetworking2.0和3.0區別很大,也是因為蘋果廢棄了NSURLConnection,而改用了NSURLSession,AFNetworking3.0實際上只是對NSURLSession所做的操作進行了高度封裝,提供更加簡潔的API供編碼調用。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容