1、使用AFN3.0 HTTPS网络请求
+(AFSecurityPolicy *)customSecurityPolicy{ //简单封装一下
//2 设置证书模式
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"mnchip" ofType:@"cer"];
NSData *cerDat = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//允许自检证书
securityPolicy.allowInvalidCertificates = YES;
//域名与服务器一致
securityPolicy.validatesDomainName = YES;
securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerDat, nil];
return securityPolicy;
}
在有网络请求的地方:
******
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//添加证书
[session setSecurityPolicy:[OperationHelper customSecurityPolicy]];
******
这样就可以了
2、关于WKWebView , 我是将Allow Arbitrary Loads in Web Content 置为 yes,然后正常使用,注意一点,要实现这个代理方法
- (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);
}}
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end
关于WKWebView就可以正常显示了
3、UIWebView 访问https 绕过证书验证的方法
在AppDelegate.m里面添加下面这些代码
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end
网友评论