Я использую WKWebView в моем текущем приложении MacOS. Но когда я захожу на самозаверяющий сертификатный сайт, он всегда получает didReceiveAuthenticationChallenge с NSURLAuthenticationMethodDefault и challenge.protectionSpace.serverTrust равен nil.
didReceiveAuthenticationChallenge: NSURLAuthenticationMethodDefault, <NSURLProtectionSpace: 0x600000046690>: Host:(null), Server:http, Auth-Scheme:NSURLAuthenticationMethodDefault, Realm:(null), Port:0, Proxy:NO, Proxy-Type:(null)
, затем происходит сбой с ошибкой:
webView didFailedProvision Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “self-signed.badssl.com” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x6000002d54c0>, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=(
"<cert(0x11fd1e500) s: *.badssl.com i: *.badssl.com>"
), NSErrorClientCertificateStateKey=0, NSErrorFailingURLStringKey=https://self-signed.badssl.com/, _kCFStreamErrorCodeKey=-9813, NSErrorFailingURLKey=https://self-signed.badssl.com/, NSUnderlyingError=0x600000d46580 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <FD76D34F-7992-4565-962A-C9864FB2807E>.<1>, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x6000030015f0>, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “self-signed.badssl.com” which could put your confidential information at risk.}
my делегат для WKWebview:
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
NSString *authMethod = [[challenge protectionSpace] authenticationMethod];
NSLog(@"webView didReceiveAuthenticationChallenge: %@, \n %@", authMethod, c);
__block NSURLCredential *credential = nil;
NSURLSessionAuthChallengeDisposition challengeType = NSURLSessionAuthChallengePerformDefaultHandling;
if ([authMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
challengeType = NSURLSessionAuthChallengeUseCredential;
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else if([authMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
if (!self.credential) {
credential =[self showCertInKeychain];
} else {
credential = self.credential;
}
challengeType = NSURLSessionAuthChallengeUseCredential;
} else if ([authMethod isEqualToString:NSURLAuthenticationMethodNTLM]){
credential = [self handleNTLMChallenge:challenge];
challengeType = NSURLSessionAuthChallengeUseCredential;
}
completionHandler(challengeType, credential);
}
Когда я пишу приложение WKdemo, использую тот же делегат в WKWebView, я могу получить didReceiveAuthenticationChallenge с NSURLAuthenticationMethodServerTrust и challenge.protectionSpace.serverTrust не равно значению nil, и все свойства ожидают.
оба приложения устанавливают ATS в качестве одного и того же параметра и Sandbox с исходящими подключениями.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Я обнаружил ту же проблему на iOS других пользователей. iOS9 WKWebview didReceiveAuthenticationChallenge вызывается с хостом null
ответ - сторонняя библиотека. Airwatch SDK
, но как я не использую Airwatch SDK. и я не знаю, как отлаживать, чтобы выяснить, какое стороннее влияние WKWebView сделал ReceiveAuthenticationChallenge.
существует одинаковый стек для обоих приложений, когда делегат является обратным вызовом.
стек кода, когда didReceiveAuthenticationChallenge является обратным вызовом