爲了縮短開發週期。咱們嘗試使用 用webview 加載html頁面的方式,實現安卓、iOS開發的同步進行。html
UIWebview 存在內存泄露問題,iOS8之後,蘋果推出了新框架Webkit,提供了替換UIWebView的組件WKWebView。web
WKWebView 在內存佔用上優化的不少。可是在實踐中發現bug:localstorage信息不一致。 A頁面和B頁面都存在 一個WKWebView。 在B頁面使用localstorage保存信息。 回到A頁面取不到最新的數據。緩存
##緣由:bash
wkwebviewconfiguration 中有個屬性 processPool,描述是:The process pool from which to obtain the view’s Web Content process.app
##解決方法: 把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 加載還慢一些。不知道是否是緩存的緣由。 求老哥們解答。優化