Enable Application cache in WKWebView

2020-01-28 08:35发布

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)

2条回答
forever°为你锁心
2楼-- · 2020-01-28 09:18

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.

查看更多
女痞
3楼-- · 2020-01-28 09:20

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

查看更多
登录 后发表回答