AFN框架 之同時相容Http和Https通訊配置

weixin_34353714發表於2016-09-22

目前很多APP都要求支援Https通訊,現在大多APP都是AFN框架之上實現的網路通訊層。以下討論了一下,AFN 框架之下,如何實現Http 和 Https相容(都支援)。(這個知識點雖然不難,但是網上很少有一試就能成功的帖子)

現在網上有很多“iOS9與XCode7中不能使用http連線的解決辦法”的貼子, 做法如下:

  1. 在專案左側找到Info.plist檔案,可以通過Filter來搜尋
  2. 在右側點選Add Row新增NSAppTransportSecurity,型別為Dictionary,然後再新增子專案NSAllowsArbitraryLoads類行為 Boolean值為YES
    按照以上的操作,的確實現了http的支援。但是如果是通過第三方機構生成SSL數字證書比如https://www.wosign.com/;要實現http,https在AFN構架下同時相容還不行。(網上有一些通過生成證書,放在App裡,實現https的通訊,個人不建議使用此用方案,因為AFN的官網上,指出了修改info.plist檔案來實現https通訊。個人按照生成證書,除錯AFN框架下的Https通訊,並沒有成功)
    建議直接用文字工作,開啟Info.plist這個xml檔案。直接配置如下圖資訊:
3102958-c86f74fbaea7a9c3.png
20160727111050083.png

解釋一下:
1> IOS9下,對http通訊支援需要新增:

<key>NSAllowsArbitraryLoads</key>  
<true/> 

2> 如果域名是www.baidu.com的介面,在AFN框架下,要支援https訪問,需要新增:

<key>NSExceptionDomains</key>  
<dict>  
    <key>www.baidu.com</key>  
<dict>  
<!--Include to allow subdomains-->  
<key>NSIncludesSubdomains</key>  
<true/>  
<!--Include to allow HTTP requests-->  
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>  
<true/>  
<!--Include to specify minimum TLS version-->  
<key>NSTemporaryExceptionMinimumTLSVersion</key>  
<string>TLSv1.2</string>  
</dict>  
</dict> 

如果Https通訊中,SSL數字證書是自己生成的AFN示例程式碼如下

-(void)httpsForPrivateCer {  
      
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];  
    // 是否允許,NO-- 不允許無效的證書  
    [securityPolicy setAllowInvalidCertificates:YES];  
    [securityPolicy setValidatesDomainName:NO];  
      
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
    manager.requestSerializer.timeoutInterval = 20;  
      
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
    manager.securityPolicy = securityPolicy;  
  
    [manager GET:URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){  
          
        NSString *result = [[ NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];  
          
        NSLog(@"%@",result);  
          
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
          
        NSLog(@"error come here");  
          
    }];  
      
}  

如果是通過第三方機構生成SSL數字證書比如

-(void)httpsForPublicCer {  
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];  
    // 是否允許,NO-- 不允許無效的證書  
    [securityPolicy setAllowInvalidCertificates:YES];  
    [securityPolicy setValidatesDomainName:NO];  
      
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
    manager.requestSerializer.timeoutInterval = 20;  
      
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
      
    manager.securityPolicy = securityPolicy;  
      
    [manager POST:@"https://www.baidu.com/home/subscribe/data/manlotteryuserdata?indextype=manht&_req_seqid=0x933251bb0002c2f5&asyn=1&t=1458805039282&sid=18880_18285_1426_17943_18205_17000_15718_12187" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){  
          
        NSString *result  =[[ NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];  
          
        NSLog(@"%@",result);  
          
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
          
        NSLog(@"error come here");  
          
    }];  
} 

相關文章