2017年1月1日"苹果iOS强制要求HTTPS连接",来提高数据传输之间的安全性,虽然之后又把时间推迟至待定,但支持HTTPS是迟早的事,是否支持HTTPS直接影响APP能否在苹果商店顺利上架。
本文只讲解iOS APP端支持https的操作流程
-
首先
在info.plist
中,增加App Transport Security Settings
的Allow Arbitrary Loads
,并把Allow Arbitrary Loads
的value
设置为YES
,如图:
-
NSURLConnection设置支持https
在iOS 9 的更新中,NSURLConnection被废弃由NSURLSession 取代,所以本身是不建议大家继续用这个类做网络请求的(同样也有AFNetWorking 2.x版本),但是考虑到一些旧程序,也不能说改就改,说替换就替换的,所以还是需要普及一下,如果用到了NSURLConnection你需要怎么做。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告诉服务器,客户端信任证书
// 创建凭据对象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 告诉服务器信任证书
[challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
}
}
-
NSURLSession设置支持https
现在推荐使用的就是NSURLSession来处理相关的网络请求了,如果使用系统自带的类,可以参考如下代码:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
// 判断是否是信任服务器证书
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告诉服务器,客户端信任证书
// 创建凭据对象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 通过completionHandler告诉服务器信任证书
completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
}
NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}
-
使用AFNetWorking 2.x版本设置支持https
考虑到这个版本,我们还可以使用AFHTTPRequestOperationManager这个类来处理网络请求。所以我们要做的就是给这个类,设置一些参数,让它可以支持https的请求,代码如下:
支持https(校验证书,不可以抓包):
// 1.初始化单例类
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.设置证书模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
mgr.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:cerData, nil];
// 客户端是否信任非法证书
mgr.securityPolicy.allowInvalidCertificates = YES;
// 是否在证书域字段中验证域名
[mgr.securityPolicy setValidatesDomainName:NO];
支持https(不校验证书,可以抓包查看):
// 1.初始化单例类
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.设置非校验证书模式
mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
mgr.securityPolicy.allowInvalidCertificates = YES;
[mgr.securityPolicy setValidatesDomainName:NO];
-
使用AFNetWorking 3.x版本设置支持https
在Xcode7.0之后,苹果废弃了NSURLConnection方法,数据请求使用NSURLSession,作为网络请求类第三方库使用量最大的AFN也及时的更新的新的版本——AFN 3.0版本。新的版本的里废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager,转而使用基于NSURLSession封装的AFHTTPSessionManager了。
支持https(校验证书,不可以抓包):
// 1.初始化单例类
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 注意写法的变化
manager.securityPolicy= [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
// 2.设置证书模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];
// 客户端是否信任非法证书
manager.securityPolicy.allowInvalidCertificates = YES;
// 是否在证书域字段中验证域名
[manager.securityPolicy setValidatesDomainName:NO];
支持https(不校验证书,可以抓包查看):
// 1.初始化单例类
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.设置非校验证书模式
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager.securityPolicy setValidatesDomainName:NO];