在plist文件中設置Allow Arbitrary Loads in Web Content 置為 YES,并實現wkwebView下面的代理方法
oc:
- (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);
}
swift:
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
? ? ? ? if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
? ? ? ? ? ? let card:URLCredential = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)
? ? ? ? ? ? completionHandler(URLSession.AuthChallengeDisposition(rawValue:2)!,card)
? ? ? ? }
? ? }
原文:https://blog.csdn.net/wz_yinglong/article/details/77507262