背景
項(xiàng)目中使用了WKWebView替換了之前的UIWebView,牽扯到Hybird開發(fā),我們需要和H5交互,所以用到了WKWebViewConfiguration 中的 WKUserContentController
所以初始化代碼如下
WKUserContentController *userContentController = [[WKUserContentController alloc] init]; [userContentController addScriptMessageHandler:self name:GetKeyiOSAndroid_Action]; [userContentController addScriptMessageHandler:self name:Upload_Action]; // WKWebView的配置 WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; configuration.userContentController = userContentController; _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; _webView.navigationDelegate = self; _webView.UIDelegate = self;
GetKeyiOSAndroid_Action Upload_Action 分別是H5通過message handler的方式來調(diào)用OC的兩個(gè)方法。
這時(shí),就已經(jīng)發(fā)生了隱患,因?yàn)?/p>
[userContentController addScriptMessageHandler:self name:GetKeyiOSAndroid_Action];
這里userContentController持有了self ,然后 userContentController 又被configuration持有,最終唄webview持有,然后webview是self的一個(gè)私有變量,所以self也持有self,所以,這個(gè)時(shí)候有循環(huán)引用的問題存在,導(dǎo)致界面被pop或者dismiss之后依然會(huì)存在內(nèi)存中。不會(huì)被釋放
當(dāng)然如果你只是靜態(tài)界面,或者與H5的交互的內(nèi)容僅限于本頁面內(nèi)的內(nèi)容,其實(shí)只是單純的內(nèi)存泄漏,但是,如果此時(shí)和H5的交互方法中牽扯到全局變量,或者全局的一些內(nèi)容,那么就不可控制了。
我發(fā)現(xiàn)這個(gè)問題是因?yàn)槲覀僿eb頁面會(huì)監(jiān)聽token過期的和登錄狀態(tài)改變的通知,然后會(huì)刷新界面,并且重新發(fā)送請求,這一系列動(dòng)作中會(huì)和用戶的全局信息進(jìn)行交互,所以在訪問一個(gè)web頁面后,切換賬號(hào)登錄時(shí)會(huì)發(fā)現(xiàn)有之前訪問過的web頁面請求發(fā)出,并且因?yàn)閠oken不同報(bào)了token過期的錯(cuò)誤,所以導(dǎo)致登錄后誤操作為token過期,緊接著被踢到登錄界面。
通過charles抓包發(fā)現(xiàn),這些web頁面都是在切換登錄賬號(hào)欠訪問的所有界面,所以,鎖定問題時(shí)web頁面依舊存在,在切換登錄后收到了登錄狀態(tài)改變的通知,重新刷新了界面導(dǎo)致請求發(fā)出并返回報(bào)錯(cuò),進(jìn)而出現(xiàn)登錄后被踢出的bug。
解決方案:
既然是循環(huán)引用,那么必須破除一邊的強(qiáng)引用,改為弱引用,或者直接去除引用。思路明朗了。。
嘗試1:
id __weak weakSelf = self;WKUserContentController *userContentController = [[WKUserContentController alloc] init];[userContentController addScriptMessageHandler:weakSelf name:GetKeyiOSAndroid_Action];
思路效仿block , 結(jié)果失敗
嘗試2:
在viewWillDisappear / viewDidDisappear 生命周期方法中調(diào)用
[_webView.configuration.userContentController removeAllUserScripts];
這算一個(gè)腦抽的嘗試,看文檔說明就懂了。自行略過
嘗試3:
不在初始化時(shí)添加ScriptMessageHandler, 而是和Notificenter/KVC一個(gè)思路
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_webView.configuration.userContentController addScriptMessageHandler:self name:GetKeyiOSAndroid_Action]; [_webView.configuration.userContentController addScriptMessageHandler:self name:Upload_Action];}- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_webView.configuration.userContentController removeScriptMessageHandlerForName:GetKeyiOSAndroid_Action]; [_webView.configuration.userContentController removeScriptMessageHandlerForName:Upload_Action];}
結(jié)果成功
小結(jié):
之前在使用WKWebView的時(shí)候很多blog的內(nèi)容都只是說了怎么添加Message Handler 但是并沒有高速大家有這個(gè)內(nèi)存泄漏的風(fēng)險(xiǎn),如果你只是頁面內(nèi)的數(shù)據(jù)調(diào)用你壓根都不會(huì)發(fā)現(xiàn)這個(gè)問題。
此坑已填!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注