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

5,169 阅读1分钟

为了缩短开发周期。我们尝试使用 用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变为单例共享

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;
	}

##疑惑 使用WKWebview 感觉比 UIWebview 加载还慢一些。不知道是不是缓存的原因。 求老哥们解答。