WKWebView 允许 native 介入到 HTTP 的验证流程,类似于 URLSession 一样对 Challenge 进行校验,具体代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler { if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); return; } SecTrustResultType result; int err = SecTrustEvaluate(challenge.protectionSpace.serverTrust, &result); if (err) { completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); return; } if (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed) { NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust]; completionHandler(NSURLSessionAuthChallengeUseCredential,credential); return; } else { completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); return; } }
|
参考链接
iOS中HTTP/HTTPS授权访问(二)
URL加载系统之四:认证与TLS链验证