Is there a way to implement similar funcitonality for a WKWebView?
Yes, via the authentication challenge handler. Your handler will be called with a
NSURLAuthenticationMethodServerTrust
authentication challenge. You can do whatever trust evaluation you want and then, when you’re done, call the completion handler with one of the
NSURLSessionAuthChallengeDisposition
values to indicate your result. This typically means
NSURLSessionAuthChallengeUseCredential
to allow the request or
NSURLSessionAuthChallengeCancelAuthenticationChallenge
to deny it.
Keep in mind that this delegate is async, so you can actually put up UI if you want. However, I strongly recommend that you not pass this decision on to the user, but instead come up with a trust evaluation strategy that can securely check the identity of your portal without user interaction.
For more background on this see Technote 2232 HTTPS Server Trust Evaluation. This technote hasn’t been updated for WKWebView, so you won’t find details on that there, but the WKWebView authentication challenge mechanism is virtually identical to the NSURLSession one.
IMPORTANT Two things to note here:
WKWebView’s authentication challenge mechanism was introduced with WKWebView itself, back in iOS 8. However, this didn’t work for
NSURLAuthenticationMethodServerTrust
until iOS 9.Moreover, it still does not work for
NSURLAuthenticationMethodClientCertificate
challenges. That’s unlikely to be a problem for you but it’s an ongoing pain point for other developers.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"