NSURLProtocol in WKWebView

How to provide custom response for my network requests in WKWebView?

I know it is possible in UIWebView using NSURLProtocol, butnot with WKWebView. So is there any alternative in WKWebView to achieve this?

Replies

WKWebView does all of its networking out of process, which means it does not ‘see’ your NSURLProtocol subclass.

What’s the goạl of your NSURLProtocol here? The NSURLProtocol-under-UIWebView thing was always a bit of a hack, and in many cases WKWebView provides better techniques with real APIs.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi,


We are trying to port our Cordova app to WKWebView. But the Cordova File plug-in use a custom scheme to work. With UIWebView, this plug-in was intercepting URLs with custom scheme using NSURLProtocol to load related files (local resources files). It seem that it is not possible to do that with WKWebView, and it is a blocker for us.

There is another strategy to do that in WKWebView?

Do we need to wait iOS 11 to have this kind of feature?


Thanks!

There is another strategy to do that in WKWebView?

That depends on how you load the resources. If you’re in control of the JavaScript code that loads the resources, you can take advantage of WKWebView nice JavaScript/native integration points. However, if the resources are loaded by the web view itself, you really need an NSURLProtocol and that’s hard to avoid.

Do we need to wait iOS 11 to have this kind of feature?

You can certainly file an enhancement request for the features you need. However, if you were in your shoes I’d discuss this with the Cordova folks; it seems like they should have a strategy for dealing with the reality of WKWebView.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo


We are using UIWebView + NSUrlProtocol to display content offline.

Images are preloaded in the app filesystem and served with URLs that are "catched" by NSUrlProtocol and served to the UIWebView as if the user is online.


We cannot rely on web traditional APIs for that (thinking of HTML5 AppCache) because it is too limited in size for this purpose.


Any idea to achieve this with WKWebView ?


Thanks


Lionel

Any idea to achieve this with WKWebView ?

As things currently stand there’s no direct way to achieve this goal with WKWebView. If you have control over the HTML content then you could customise it so that it works when loaded directly off the local file system.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 5 Jun through to Fri, 9 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

Any update or new information on this issue?
I’m not up to speed on all the latest news. My advice is that you create a new thread with the details of your specific situation and then tag it with WebKit (this thread has a very generic tag because it was migrated from the old forums which didn’t have a good place for WKWebView questions).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"