纸上得来终觉浅,绝知此事要躬行。

0%

解决WKWebview-localstorage-存取信息不一致问题

为了缩短开发周期。我们尝试使用 用webview 加载html页面的方式,实现安卓、iOS开发的同步进行。

UIWebview 存在内存泄露问题,iOS8以后,苹果推出了新框架Webkit,提供了替换UIWebView的组件WKWebView。

WKWebView 在内存占用上优化的很多。但是在实践中发现bug:localstorage信息不一致。
场景:A页面和B页面都存在 一个WKWebView。 在B页面使用localstorage保存信息。 回到A页面取不到最新的数据。

原因:

wkwebviewconfiguration 中有个属性 processPool,描述是:The process pool from which to obtain the view’s Web Content process.

解决方法:

把config中的processPool变为单例共享

1
2
3
4
5
6
7
8
9
10
11
12
WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
//使用单例 解决locastorge 储存问题
wkConfig.processPool = [hrWkWebViewController singleWkProcessPool];

_wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkConfig];

+ (WKProcessPool *)singleWkProcessPool{
AFDISPATCH_ONCE_BLOCK(^{
sharedPool = [[WKProcessPool alloc] init];
})
return sharedPool;
}