1、使用AFN3.0 HTTPS網(wǎng)絡(luò)請求
+(AFSecurityPolicy *)customSecurityPolicy{ //簡單封裝一下
//2 設(shè)置證書模式
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"mnchip" ofType:@"cer"];
NSData *cerDat = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//允許自檢證書
securityPolicy.allowInvalidCertificates = YES;
//域名與服務(wù)器一致
securityPolicy.validatesDomainName = YES;
securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerDat, nil];
return securityPolicy;
}
在有網(wǎng)絡(luò)請求的地方:
******
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//添加證書
[session setSecurityPolicy:[OperationHelper customSecurityPolicy]];
******
這樣就可以了
2、關(guān)于WKWebView , 我是將Allow Arbitrary Loads in Web Content 置為 yes,然后正常使用,注意一點,要實現(xiàn)這個代理方法
- (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
關(guān)于WKWebView就可以正常顯示了
3、UIWebView 訪問https 繞過證書驗證的方法
在AppDelegate.m里面添加下面這些代碼
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end