WKWebView and NSURLCache to serve local content
Asked Answered
A

1

6

My app loads a very big webapp inside a UIWebView.

I have written a NSURLCache extension "LocalCache".

This LocalCache extension intercepts my webapp loading, and serves all requested files from a local app bundle that is encrypted. This logic is implemented in NSURLCache's cachedResponseForRequest method. So the app is served 100% locally:

class LocalCache:NSURLCache {
    override func cachedResponseForRequest( request: NSURLRequest) -> NSCachedURLResponse? {
        if (request points to my domain) {
            get file from bundle
            decrypt it
            return local copy
        } else {
            return super.cachedResponseForRequest(request)
        }
    }
}

NSURLCache.setSharedURLCache(LocalCache());

I'd like to port this functionality to WKWebkit. I wonder if there's a way to implement something similar, because unfortunately, as you probably know, WKWebView does not use the Cocoa stack with NSProtocol, NSUrl, NSUrlCache .... rendering my current approach useless.

So, can something similar be accomplished with WkWebView?

Note: The fact that UiWebView "thinks" that my app comes from a remote server is key for the application: If I just load the application locally, ie. file://, there is a lot of stuff that doesn't work, for example, YouTube Videos, as youtube api complains that "file://" is not an approved origin. So the WkWebView solution I'm looking for has to be related to intercepting the cache rather than injecting local javascript.

Aaren answered 11/4, 2015 at 3:40 Comment(3)
"WKWebView makes requests and renders content out-of-process, meaning your app does not hear the requests they make." https://mcmap.net/q/380702/-wkwebview-and-nsurlprotocol-not-workingOverwork
@Aaren have you found any solutions to this? I'm have a similar problem. I'm thinking of trying WKUserScript.Dorri
@Aaren I am having similar requirement, I am using a WKWebView and I have to cache the response. Did you get any solution for this.Harebell
G
2

I was in a very similar position a few months ago (using NSURLProtocol / NSURLCache and wanting to migrate from UIWebView to WKWebView), and what I ended up doing was to use a local HTTP server to serve my files/requests (https://github.com/mattstevens/RoutingHTTPServer).

This also helped me deal with a different issue, which was related to loading HTML5 videos on the UIWebView, because some of the range requests were not being intercepted by the UIWebView / NSURLCache.

Grounder answered 6/1, 2017 at 10:3 Comment(3)
Hi, that's a nice idea, I have stopped IOS development for a while but will eventually get into it again. About that solution, for me it can be a problem that the app domain will be local, and some services I use refuse access to their API if the request doesn't come from an app running in a host. That's why the other solution was pretty cool, WebView thought it came from the internet!Aaren
Besides, I don't know if it has changed since then, but I got a little disappointed with the supposed "performance boost" promised by WkWebView. I run a pretty demanding app and would say the performance is about the same!Aaren
Even though one of my motivations for migrating was the supposed "performance boost", my main worry was that UIWebView has been deprecated for a while and I feared it could "stop working" or even be removed in future versions. It also doesn't have some of the heuristics to distinguish long taps from double taps (so unless you use JS libraries to get around that, you always get that nasty 300ms delay on taps), which from what I read WKWebView already has.Flotation

© 2022 - 2024 — McMap. All rights reserved.