步驟如下:
一驶兜、新建一個(gè)NSURLRequest的類別
二、在這個(gè)類別中新增兩個(gè)類方法偷俭,如下:
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
return YES;
}
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host {
}
注:這兩個(gè)方法是系統(tǒng)的私有方法
浦箱。
三大渤、實(shí)現(xiàn)WKWebView的代理方法(- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler)糠涛,實(shí)現(xiàn)代碼如下:
#pragma mark - wkwebview信任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);
}
}
四、在加載webView的URL之前調(diào)用
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"https"];
這行代碼即可兼犯。