Enable Application cache in WKWebView

后端 未结 2 447
太阳男子
太阳男子 2020-11-30 07:52

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 &

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 08:41

    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

提交回复
热议问题