iOS中wkwebView内存泄漏与循环引用问题详解

2020-01-21 06:41:36王旭

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到IOS开发频道。