13

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)

Clement Prem
  • 3,112
  • 2
  • 23
  • 43

2 Answers2

10

Update 2022

According to one of the comments below, this hack doesn't work anymore.


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

Clement Prem
  • 3,112
  • 2
  • 23
  • 43
  • It's not that they can't, I think that they just won't. My guess is that they want us to handle offline ourselves rather than using the appcache. – Darkshore Grouper May 19 '15 at 06:27
  • 1
    You got to love Apple for this! – Elad Jun 02 '16 at 13:22
  • Hi Clement, i was wondering if you figured out a way to bypass the 25mb appcache limit imposed (by apple?), if i exceed that i never get the alert dialog asking me if i want to assign more space like in safari and the appcache simply fails to install, maybe there's a way to programmatically define that?, i looked at Webkit source code but couldn't find where they set it. – dmm79 Dec 22 '16 at 12:33
  • @dmm79 Hello, did you ever find out what the limit was or where it was defined? Thanks :) – Luke Jan 29 '20 at 16:15
  • @Luke no, i even asked apple developer support way back then and they simply said it wasn't supported in WkWebView :| – dmm79 Jan 30 '20 at 18:21
  • @dmm79 ah typical, hehe! Thanks for the quick reply! – Luke Jan 30 '20 at 18:35
  • AppCache has been deprecated in all browsers for years and Safari officially deprecated it in 2018. Do not rely on this for anything in 2022. https://bugs.webkit.org/show_bug.cgi?id=181764 – oligofren Feb 14 '22 at 09:41
3

According to this tweet from at Apple, as of iOS 10, App Cache is now supported in WKWebView:

https://twitter.com/andersca/status/743259582252879872

...as does this WebKit bug report:

https://bugs.webkit.org/show_bug.cgi?id=152490

I've tested this in WKWebView using this site:

http://webdbg.com/test/appcache/

and can confirm it works as expected both in the iOS Simulator and on devices running iOS 10.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • 1
    No native-side code changes are required to support App Cache, but obviously you need to make the necessary web content changes. General information about this can be found in Apple's HTML5 Offline Application Cache guide: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/OfflineApplicationCache/OfflineApplicationCache.html – Andrew Ebling Jul 24 '17 at 09:03
  • @AndrewEbling Does App Cache cache everything? For example, does it cache AJAX requests and responses? – Cloud9999Strife Feb 27 '18 at 12:49
  • 1
    AppCache has been deprecated for years and Safari officially deprecated in 2018. Do not rely on this for anything in 2022. https://bugs.webkit.org/show_bug.cgi?id=181764 – oligofren Feb 14 '22 at 09:40