1. 程式人生 > >iOS開發---WKWebView載入不受信任的https

iOS開發---WKWebView載入不受信任的https

1.描述:因公司域名臨時出現問題,所以專案中引用到了IP地址加埠號去請求資料,因而造成在wkwebView中某些網址打不開,檢視錯誤是因為伺服器證書無效,實際就是不受信任;
2.解決辦法:在plist檔案中設定Allow Arbitrary Loads in Web Content 置為 YES,並實現wkwebView下面的代理方法,就可解決
- (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);
        
    }
}