I have built a class that intercepts UIWebView resource loading by subclassing NSURLCache and overriding cachedResponseForRequest:, similar to this example. Basically, if the requested resource is of type .css, I look for it in a local caches directory and if found, I load the local version.
The problem is that I now want to be able to specify which css files to look for in local cache, by providing a specific scheme that my app knows about. But when I change the scheme to something like:
myapp://www.myhost.com/static/default.css
then cachedResponseForRequest: no longer gets called when the html is being loaded. Does anyone know why this, or how I can enable this method for my url with a specific scheme?
The key is to register your own subclass of NSURLProtocol, which can respond to your custom scheme in order to load those special resources. I used the following two methods:
To register, call
[MyAppURLProtocol enable]from the app delegate. You also need to override all other required methods defined in the NSURLProtocol reference, which ultimately forces you to load the data yourself..The reason why it is necessary to register your own url protocol is documented by apple as: