So your site has a self-signed ssl certificate and you want to visit it in a web view? I have done this using a UIWebView (I am assuming it works for WKWebView too).
Before you attempt to load your page in the view, you need to make a call via NSURLConnection and explicitly trust the self-signed ssl cert. You will get notified via NSURLConnectionDelegate when a secure connection is being made with an invalid cert, where you can optionally trust it in your code or not.
Obj-C:
Code:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && [challenge.protectionSpace.host isEqualToString:@"myDomain.com") {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
}
Swift:
Code:
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "myDomain.com" {
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
} else {
challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}
The code above will accept the connection if the host matches what I expect it to or else if will do the default handling. This way your app won't accept all invalid certs, just the one from your host. Note that this isn't the best option, you probably want to do further verification to ensure you are connection to the right host
This method works out really well for our app, as we have to hit a REST login endpoint to login in the user before we attempt to show the webpage, and that initial login call causes our ssl cert trust to get activated and then the web view can access our site without issue.
Hopefully that helps!