马上就2017年了,据说元旦apple就强制HTTPS了,就像之前强制IPV6一样任性,好方~~~ 不到万不得已,项目经理不舍得买CA证书,于是就开始了坑爹的自建证书HTTPS之旅。 写在前面的提醒 ① 还没到2017,苹果还没确切的说明对HTTPS的要求,所以目前还不确定自建证书到底可不可以通过审核。 ② 网上很多人说自建证书必须禁掉ATS才能正常访问(App Transport Security Settings -> Allow Arbitrary Loads -> YES),但是在我的项目中没有禁掉ATS自建证书是可以正常使用的,欢迎大家讨论原因。 一、配置证书 1、服务器端那边配置https有一堆证书,试了好久才确定是哪个。 正常说应该是服务器那边最开始的root/ca证书,我们最终需要的格式是.der格式(AF3.0以上)或者.cer格式(AF3.0以下),给过来的格式根据情况不一定,如果是我们需要的格式最好,不是的话大家自行或者让服务端帮转。 我这边情况是给过来后缀名为.crt,拿到手后要转成.der格式或者.cer格式。 2、.crt转.der方法(终端): openssl x509 -in /Users/mac/Desktop/ca.crt -out /Users/mac/Desktop/ca.der -outform DER .crt转.cer方法(终端): openssl x509 -in /Users/mac/Desktop/ca.crt -out /Users/mac/Desktop/ca.cer -outform DER 3、如果证书用错了,iOS这边会报“-999,已取消”的错误,(网上很多说如果服务器做的是双向验证也会报这个错误,大家根据实际情况来试,我之前就以为服务器是双向验证试了好久,双向验证方法参考:https双向认证 iOS客户端处理 建议能做单向都别给彼此找麻烦了。。。) 二、导入证书 1、直接把.der证书拉进项目 2、网上有人说要加这一步,否则证书有一定几率没加进来,不知道有没有用,为保险加一下吧。 导入证书 三、AF3.0单向验证设置 1、在你封装的网络类里添加下面代码: - (AFSecurityPolicy*)getCustomHttpsPolicy:(AFHTTPSessionManager*)manager{ //https 公钥证书配置 NSString *certFilePath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"der"]; NSData *certData = [NSData dataWithContentsOfFile:certFilePath]; NSSet *certSet = [NSSet setWithObject:certData]; AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:certSet]; policy.allowInvalidCertificates = YES;// 是否允许自建证书或无效证书(重要!!!) policy.validatesDomainName = NO;//是否校验证书上域名与请求域名一致 return policy; } 复制代码 2、在网络请求前设置一行代码,然后其他正常网络请求 AF3.0配置 四、WKWebView设置支持https 1、要实现下面代理方法,当然别忘了设置代理navigationDelegate // https 支持 - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{ if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust]; completionHandler(NSURLSessionAuthChallengeUseCredential,card); } } 复制代码 2、web端当然所有的接口路由都需要从http改到https 五、SDWebImage支持https 1、感谢丨Majestic灬磊 简单粗暴的方法,在此引用: - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; 复制代码 options : SDWebImageAllowInvalidSSLCertificates 直接跳过验证证书就可以啦! 2、建议如果不想大改项目可以直接到 UIImageView+WebCache.m 中 把用到的几个方法改掉就好了 - (void)sd_setImageWithURL:(NSURL *)url { [self sd_setImageWithURL:url placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates progress:nil completed:nil]; } - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { [self sd_setImageWithURL:url placeholderImage:placeholder options:SDWebImageAllowInvalidSSLCertificates progress:nil completed:nil]; } 复制代码 到此用自建证书迁移到HTTPS就完毕了,跑了一遍整个项目暂时没有什么问题,如果apple不允许自建证书还请第一时间告知/(ㄒoㄒ)/~~ 参考: iOS 中 AFNetworking HTTPS 的使用 https双向认证 iOS客户端处理 iOS https自建证书 请求服务器 和 WKWebView SDWebImage访问HTTPS站点获取图片资源失败解决办法 原文作者:vincent涵 来源:开发者头条