概述
從上圖可以看出,AFNetwoking框架是一個(gè)比較簡(jiǎn)潔的框架,主要分了幾個(gè)部分:
1.->網(wǎng)絡(luò)通信模塊:NSURLSession
2->網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)模塊:Reachability
3->網(wǎng)絡(luò)通信安全模塊:Security
4->網(wǎng)絡(luò)通信序列化模塊:Serialization
5->對(duì)UIKit框架擴(kuò)展部分:UIKit(以Catagory形式添加特性)
在這幾個(gè)模塊中,AFNetwoking的核心模塊是通信模塊,在通信模塊有兩個(gè)類(lèi),AFHttpSessionManager和AFURLSessionManager,其中前者繼承于后者,是對(duì)于HTTP的專(zhuān)一化封裝處理。AFNetworking 3.0其實(shí)只是對(duì)NSURLSession做了封裝處理
/*使用NSURLSession和使用AFNetworking做網(wǎng)絡(luò)請(qǐng)求在實(shí)現(xiàn)過(guò)程中有什么區(qū)別*/
*使用區(qū)別
1.使用進(jìn)行網(wǎng)絡(luò)請(qǐng)求
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請(qǐng)求
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請(qǐng)求
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.創(chuàng)建管理者對(duì)象
AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];
//2.確定請(qǐng)求的URL地址
NSURL* url =[[NSURL alloc]initWithString:@"hrrps:www.com.cn"];
//3.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest* request = [NSURLRequest requestWithURL:url];
//4.下載任務(wù)
NSURLSessionDownloadTask* task = [sessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//打印下載進(jìn)度
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//下載地址----targetPath
//設(shè)置下載路徑,通過(guò)沙盒獲取緩存地址,最后返回NSURL對(duì)象
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//下載完成調(diào)用的方法
}];
//開(kāi)始啟動(dòng)任務(wù)
[task resume];
}
//第一種上傳方法------通過(guò)工程中的文件進(jìn)行上傳
- (void)upload1{? ? ? ?
?//1.創(chuàng)建管理者對(duì)象??
? AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];? ?
?//2.上傳文件? ? NSDictionary* dict = @{@"key":@"value"};
//參數(shù)? ? [sessionManager POST:@"https://www.com.cn" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {
//上傳文件參數(shù)
UIImage *iamge = [UIImage imageNamed:@"123.png"];
NSData *data = UIImagePNGRepresentation(iamge);
//這個(gè)就是參數(shù)
[formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//進(jìn)度
} success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {
//請(qǐng)求成功
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//請(qǐng)求失敗
}];
}
//第二種上傳方法------通過(guò)URL來(lái)獲取路徑,進(jìn)入沙盒或者系統(tǒng)相冊(cè)等
- (void)upload2{? ? ? ?
?//1.創(chuàng)建管理者對(duì)象? ??
AFHTTPSessionManager* sessionManager = [AFHTTPSessionManager manager];??
? //2.上傳文件? ? NSDictionary* dict = @{@"key":@"value"};
//參數(shù)? ? [sessionManager POST:@"https://www.com.cn" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {
//上傳文件參數(shù)
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件地址"] name:@"file" fileName:@"1234.png" mimeType:@"application/octet-stream" error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//進(jìn)度
} success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {
//請(qǐng)求成功
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//請(qǐng)求失敗
}];
}
//對(duì)當(dāng)前網(wǎng)絡(luò)進(jìn)行監(jiān)聽(tīng)
- (void)AFNetworkStatus{
//1.創(chuàng)建網(wǎng)絡(luò)監(jiān)測(cè)者
AFNetworkReachabilityManager* manager = [AFNetworkReachabilityManager sharedManager];
/*
網(wǎng)絡(luò)的四種狀態(tài):
AFNetworkReachabilityStatusUnknown? ? ? ? ? = -1,? ? ? 未知
AFNetworkReachabilityStatusNotReachable? ? = 0,? ? ? 無(wú)網(wǎng)絡(luò)
AFNetworkReachabilityStatusReachableViaWWAN = 1,? ? ? 蜂窩數(shù)據(jù)網(wǎng)絡(luò)
AFNetworkReachabilityStatusReachableViaWiFi = 2,? ? ? WiFi
*/
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//這里是監(jiān)測(cè)到網(wǎng)絡(luò)改變的block? 可以寫(xiě)成switch方便
//在里面可以隨便寫(xiě)事件
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡(luò)狀態(tài)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"無(wú)網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窩數(shù)據(jù)網(wǎng)");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi網(wǎng)絡(luò)");
break;
default:
break;
}
}];
}
可以發(fā)現(xiàn)使用AFHTTPSessionManager進(jìn)行網(wǎng)絡(luò)請(qǐng)求大致分為了兩步:
1->創(chuàng)建一個(gè)AFHTTPSessionManager對(duì)象
2->使用這個(gè)對(duì)象調(diào)用含有block的請(qǐng)求方法
從調(diào)用上來(lái)看,AFNetworking的請(qǐng)求會(huì)更加易讀和編寫(xiě)
從對(duì)AFNetworking的源碼分析可以發(fā)現(xiàn),AFNetworking的內(nèi)部實(shí)現(xiàn)到棧低仍然是操作了原生的NSURLSession,從根本上只是對(duì)原生的NSURLSession做了封裝操作,封裝了一些序列化、通信安全等策略,提供簡(jiǎn)潔的API,方便用戶(hù)編碼。
注意的是,AFNetworking 默認(rèn)接收 json 格式的響應(yīng)(因?yàn)檫@是在 iOS 平臺(tái)上的框架,一般不需要 text/html),如果想要返回 html,需要設(shè)置 acceptableContentTypes
引入 AFSecurityPolicy 保證請(qǐng)求的安全
AFSecurityPolicy 是 AFNetworking 用來(lái)保證 HTTP 請(qǐng)求安全的類(lèi),它被 AFURLSessionManager 持有,如果你在 AFURLSessionManager 的實(shí)現(xiàn)文件中搜索 self.securityPolicy,你只會(huì)得到三條結(jié)果:
初始化 self.securityPolicy = [AFSecurityPolicy defaultPolicy]
收到連接層的驗(yàn)證請(qǐng)求時(shí)
任務(wù)接收到驗(yàn)證請(qǐng)求時(shí)
在 API 調(diào)用上,后兩者都調(diào)用了 - [AFSecurityPolicy evaluateServerTrust:forDomain:] 方法來(lái)判斷當(dāng)前服務(wù)器是否被信任,實(shí)現(xiàn)代碼
- (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);
}
}
如果沒(méi)有傳入 taskDidReceiveAuthenticationChallenge block,只有在上述方法返回 YES 時(shí),才會(huì)獲得認(rèn)證憑證 credential。
NSURLAuthenticationChallenge 表示一個(gè)認(rèn)證的挑戰(zhàn),提供了關(guān)于這次認(rèn)證的全部信息。它有一個(gè)非常重要的屬性 protectionSpace,這里保存了需要認(rèn)證的保護(hù)空間, 每一個(gè) NSURLProtectionSpace 對(duì)象都保存了主機(jī)地址,端口和認(rèn)證方法等重要信息。
在上面的方法中,如果保護(hù)空間中的認(rèn)證方法為 NSURLAuthenticationMethodServerTrust,那么就會(huì)使用在上一小節(jié)中提到的方法
- [AFSecurityPolicy evaluateServerTrust:forDomain:] 對(duì)保護(hù)空間中的 serverTrust 以及域名 host 進(jìn)行認(rèn)證
根據(jù)認(rèn)證的結(jié)果,會(huì)在 completionHandler 中傳入不同的 disposition 和 credential 參數(shù)。
自 iOS9 發(fā)布之后,由于新特性 App Transport Security 的引入,在默認(rèn)行為下是不能發(fā)送 HTTP 請(qǐng)求的。很多網(wǎng)站都在轉(zhuǎn)用 HTTPS,而 AFNetworking 中的 AFSecurityPolicy 就是為了阻止中間人攻擊,以及其它漏洞的工具。
AFSecurityPolicy 主要作用就是驗(yàn)證 HTTPS 請(qǐng)求的證書(shū)是否有效,如果 app 中有一些敏感信息或者涉及交易信息,一定要使用 HTTPS 來(lái)保證交易或者用戶(hù)信息的安全。
使用 AFSecurityPolicy 時(shí),總共有三種驗(yàn)證服務(wù)器是否被信任的方式:
typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {
AFSSLPinningModeNone,
AFSSLPinningModePublicKey,
AFSSLPinningModeCertificate,
};
AFSSLPinningModeNone 是默認(rèn)的認(rèn)證方式,只會(huì)在系統(tǒng)的信任的證書(shū)列表中對(duì)服務(wù)端返回的證書(shū)進(jìn)行驗(yàn)證
AFSSLPinningModeCertificate 需要客戶(hù)端預(yù)先保存服務(wù)端的證書(shū)
AFSSLPinningModeCertificate 也需要預(yù)先保存服務(wù)端發(fā)送的證書(shū),但是這里只會(huì)驗(yàn)證證書(shū)中的公鑰是否正確
2> allowInvalidCertificates
allowInvalidCertificates 定義了客戶(hù)端是否信任非法證書(shū)。一般來(lái)說(shuō),每個(gè)版本的iOS設(shè)備中,都會(huì)包含一些既有的CA根證書(shū)。如果接收到的證書(shū)是iOS信任的CA根證書(shū)簽名的,那么則為合法證書(shū);否則則為“非法”證書(shū)。
allowInvalidCertificates 就是用來(lái)確認(rèn)是否信任這樣的證書(shū)的。當(dāng)然,我們也可以給iOS加入新的信任的CA證書(shū)。iOS已有的CA根證書(shū),可以在這里了解到:https://support.apple.com/en-us/HT204132
3> pinnedCertificates
pinnedCertificates 就是用來(lái)校驗(yàn)服務(wù)器返回證書(shū)的證書(shū)。通常都保存在mainBundle 下。通常默認(rèn)情況下,AFNetworking會(huì)自動(dòng)尋找在mainBundle的根目錄下所有的.cer文件并保存在pinnedCertificates數(shù)組里,以校驗(yàn)服務(wù)器返回的證書(shū)。
4> validatesDomainName
validatesDomainName 是指是否校驗(yàn)在證書(shū)中的domain這一個(gè)字段。每個(gè)證書(shū)都會(huì)包含一個(gè)DomainName, 它可以是一個(gè)IP地址,一個(gè)域名或者一端帶有通配符的域名。如*.google.com, www.google.com 都可以成為這個(gè)證書(shū)的DomainName。設(shè)置validatesDomainName=YES將嚴(yán)格地保證其安全性。
5> validatesCertificateChain
validatesCertificateChain 指的是是否校驗(yàn)其證書(shū)鏈。
通常來(lái)講,一個(gè)CA證書(shū)頒發(fā)機(jī)構(gòu)有很多個(gè)子機(jī)構(gòu),用來(lái)簽發(fā)不同用途的子證書(shū),然后這些子證書(shū)又再用來(lái)簽發(fā)相應(yīng)的證書(shū)。只有證書(shū)鏈上的證書(shū)都正確,CertificateChain才算驗(yàn)證完成。
做好以上工作后,您應(yīng)該就可以正常訪問(wèn)您自己的https服務(wù)器了。如果還是有問(wèn)題請(qǐng)檢查:
(1)HTTPS服務(wù)器的正確配置。一般來(lái)說(shuō),可以使用瀏覽器打開(kāi)相同頁(yè)面來(lái)查看瀏覽器上的小鎖是否正常。
(2)是否https.cer正確打包進(jìn)了項(xiàng)目中。
總結(jié):
AFNetworking2.0和3.0區(qū)別很大,也是因?yàn)樘O(píng)果廢棄了NSURLConnection,而改用了NSURLSession,AFNetworking3.0實(shí)際上只是對(duì)NSURLSession所做的操作進(jìn)行了高度封裝,提供更加簡(jiǎn)潔的API供編碼調(diào)用。