溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS中wkwebView內存泄漏與循環引用問題的示例分析

發布時間:2021-08-19 14:41:38 來源:億速云 閱讀:190 作者:小新 欄目:移動開發

這篇文章給大家分享的是有關iOS中wkwebView內存泄漏與循環引用問題的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

解決方法

1,在做網頁端js交互的時候 我們都會這樣去添加js

[self.customWebView.configuration.userContentController addScriptMessageHandler:self name:obj];

后面也添加了 delloc

- (void)dealloc {
 [_customWebView removeObserver:self forKeyPath:@"estimatedProgress"];
 [self removeScriptMessageHandler];
}

后來發現在加載網頁的時候 pop push 多次操作 內存一直在增加,高的時候 都快200上下了,才注意到這個內存問題,
剛開始的解決方法是:

- (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 
 [self removeScriptMessageHandler];
}

后來發現問題依舊存在 delloc 依舊不走,雖然走了移除方法 ,但是在當你在pop push時候 網頁沒有移除掉原先占的內存,后來發現

[userContentController addScriptMessageHandler:self name:GetKeyiOSAndroid_Action];

這里userContentController持有了self ,然后
userContentController 又被configuration持有,
最終唄webview持有,然后webview是self的一個私有變量,
所以self也持有self,所以這個時候有循環引用的問題存在,
導致界面被pop或者dismiss之后依然會存在內存中。不會被釋放
目前想到2個辦法

1,上面我提到了 self持有self,導致的循環引用問題

我做法是重新建了一個類WKWebViewConfiguration

[[WKWebViewConfiguration alloc]init]; 

  userContentController =[[WKUserContentController alloc]init];       configuration.userContentController= userContentController; 

  webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:configuration];

重寫self方法就解決了

2,delloc 內存,

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

感謝各位的閱讀!關于“iOS中wkwebView內存泄漏與循環引用問題的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女