WKWebView and NSURLProtocol not working
Asked Answered
F

4

43

When using the old UIWebView you could catch the requests by implementing a custom NSURLProtocol. I us this to handle requests that requires authentication.

I tried the same code and it doesn't work with the new WKWebView but my protocol class isn't called at all. Is someone experiencing the same problem or is there a better way of doing authentication with the WKWebView?

Without any modifications I get a 401 response in the decidePolicyForNavigationResponse delegate function. I've also tried connection to the server with a NSURLConnection and handling the authentication with a NSURLConnectionDataDelegate. That works but the stored credentials isn't picked up by the WKWebView.

Followthrough answered 13/6, 2014 at 14:51 Comment(0)
L
56

Updated answer for iOS 11 and macOS 10.13

Since iOS 11 it is possible to declare an object that conforms to the WKURLSchemeHandler protocol and register it in the WKWebView configuration: -[WKWebViewConfiguration setURLSchemeHandler:forURLScheme:].

Old answer

WKWebView makes requests and renders content out-of-process, meaning your app does not hear the requests they make. If you are missing a functionality, now is the time to open a bug report and/or an enhancement request with Apple.

As of iOS 10.3 SDK, WKWebView is still unable to make use of custom NSURLProtocols using public APIs.


Enterprising developers have found an interesting method: +[WKBrowsingContextController registerSchemeForCustomProtocol:] It supposedly adds the provided scheme to a list of custom protocol handled schemes and should then work with NSURLProtocol.

Laniary answered 13/6, 2014 at 14:55 Comment(8)
So, so disappointing. Has Apple given any reason for this change?Dactylography
@Dactylography The out-of-process makes it difficult to implement.Bilek
Thanks for updating this for the latest iOS SDKs! Pretty disappointing it's still not supported... hope lots of radars have been opened to make this a reality.Gause
This was added in iOS 11.Clasp
No, HTTP protocols are not allowed. I have not looked if it might be possible using private API.Bilek
@RamaniHitesh Your code JS nothing to do with this issue. Please don’t spam.Bilek
Did anyone tried to submit app with using private API?Consummate
@Consummate If you are going to use private API, at least do minimal attempt to hide it. You can search Google how to do it.Bilek
U
3

WKWebView has a navigationDelegate property. If that delegate is set WKWebView will call the didReceiveAuthenticationChallenge method on that delegate if the method is implemented. You need to place your authentication code in this method. Example:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
    NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"bob"
                                                               password:@"pass"
                                                            persistence:NSURLCredentialPersistenceNone];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
Utrillo answered 7/7, 2015 at 5:44 Comment(0)
H
1

Try this approach: https://github.com/WildDylan/WKWebViewWithURLProtocol/tree/master/Example/WKWebViewWithURLProtocol

It may be uses private API - i'm not sure ;)

Hunkydory answered 20/12, 2016 at 8:20 Comment(1)
This is interesting, albeit written strangely. All that is needed is calling [WKBrowsingContextController registerSchemeForCustomProtocol:@"schemes"];Bilek
T
0

If you're using URLProtocol just for authentication, there is some other way to achieve that.

Like when u got a error code -1202 from didFailLoad, and make a URLConnection to do the authentication thing, reload the page after.

Actually I need to using URLProtocol with WKWebView also, lol~

Tankage answered 25/6, 2014 at 2:11 Comment(2)
I tried that solution but WKWebView didn't pick up the credentials after reloadFollowthrough
sorry to hear about that ,if u got any news, pls info meTankage

© 2022 - 2024 — McMap. All rights reserved.