Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Application cache in WKWebView

I am aware that offline application cache is not supported in iOS WKWebView.

This is enabled in Safari, so I searched webkit project for the responsible code & found this

WKPreferences

- (void)_setOfflineApplicationCacheIsEnabled:(BOOL)offlineApplicationCacheIsEnabled;

Anyone familiar with this method? is it possible to enable app cache in iOS by accessing this private methods? (I am not going to ship the app to Appstore)

like image 227
Clement Prem Avatar asked Apr 27 '15 10:04

Clement Prem


People also ask

How to enable AppCache in wkwebview?

After initializing the WKWebView, enable the appcache by calling the above method in the following object [_wkWebView.configuration.preferences _setOfflineApplicationCacheIsEnabled:YES]; It will create the ApplicationCache.db file in the Cache directory and allow the web app to work offline. Warning : 2.5.

How do I create a wkwebviewconfiguration?

You create a WKWebViewConfiguration object in your code, configure its properties, and pass it to the initializer of your WKWebView object. The web view incorporates your configuration settings only at creation time; you cannot change those settings dynamically later.

Do web views cache?

No. Web views do cache but, as with all web caching, there’s no guarantee that they will cache a specific resource. I’m not sure why the web view hasn’t cached the resource you care about — I expect it’d make sense if you drilled down into the caching logic — but ultimately the decision as to what gets cache is the web view’s to make.

How can I force a re-download of my WebView?

Your dynamic query string is the most reliable trick to get WKWebView to force a re-download and avoid the cache. You seem stuck between a rock and a hard place here. However, you did mention "webview within the application". Since you do have everything inside one file, have you considered loading your webview directly from an HTML string?


1 Answers

Yes, we can enable App cache by accessing private API

Create a category for WKPreferences and add to following method signature.

@interface WKPreferences (MyPreferences)
- (void)_setOfflineApplicationCacheIsEnabled:(BOOL)offlineApplicationCacheIsEnabled;
@end

(I tried performSelector:withObject: but it didn't work. No idea why)

After initializing the WKWebView, enable the appcache by calling the above method in the following object

  [_wkWebView.configuration.preferences _setOfflineApplicationCacheIsEnabled:YES];

It will create the ApplicationCache.db file in the Cache directory and allow the web app to work offline.

Warning :

2.5. Apps that use non-public APIs will be rejected

like image 56
Clement Prem Avatar answered Sep 20 '22 01:09

Clement Prem