Invalid WKWebView https certificate.

Here is the code I use to resolve the certificate:

@interface NSURLRequest(DummyInterface) + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host; + (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host; @end @implementation NSURLRequest(DummyInterface) + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return [host isEqualToString:@"mysite.com"]; } @end 

And I initialize my WKWebView as follows:

 NSURL *urlReq = [NSURL URLWithString:@"mysite.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:urlReq]; [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[urlReq host]]; WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; mainWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration]; [mainWebView setNavigationDelegate:self]; [mainWebView loadRequest:request]; 

It works fine for an http site, but I have this error using https:

The certificate for this server is not valid. You might be connecting to a server that pretends to be "mysite.com" that could put your sensitive information at risk.

It worked when I used UIWebView and implemented the function "canAuthenticateAgainstProtectionSpace", but now I don’t understand what I need to do.

Am I missing something or can WKWebView not handle HTTPS?

+5
source share
2 answers

Try it, it worked for me

 - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { NSLog(@"Allowing all"); SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; CFDataRef exceptions = SecTrustCopyExceptions (serverTrust); SecTrustSetExceptions (serverTrust, exceptions); CFRelease (exceptions); completionHandler (NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]); } 

And do not forget to add to Info.plist

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 
+5
source

I don’t know what problems there are, but there are reports that WKWebView has problems with SSL: https://code.google.com/p/chromium/issues/detail?id=423444#c3

+1
source

Source: https://habr.com/ru/post/1205286/


All Articles