使用WKWebView時(shí)一般會(huì)遇到原生與JS交互的問(wèn)題。在JS調(diào)用原生時(shí)需要使用WKUserContentController類(lèi)的addScriptMessageHandler: name:方法監(jiān)聽(tīng)JS事件,但在添加該監(jiān)聽(tīng)后頁(yè)面退出時(shí)會(huì)發(fā)現(xiàn)控制器不走dealloc方法,內(nèi)存不釋放。
#import <WebKit/WebKit.h>
@property (nonatomic ,strong) WKWebView *webView;
- (WKWebView *)webView {
if (!_webView) {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self name:@"doShare"];
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) configuration:config];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.allowsBackForwardNavigationGestures = YES;
}
return _webView;
}
內(nèi)存不釋放原因分析:
userContentController持有了self ,userContentController 又被configuration持有,configuration被webView持有,然后webView作為self的一個(gè)私有變量被self持有,最終導(dǎo)致了self的循環(huán)引用。
解決思路:
通過(guò)分析得知控制器不走dealloc方法的原因?yàn)檠h(huán)引用,那么就必須將一方改為弱引用或者直接去除引用。
解決方式1:
使用addScriptMessageHandler: name:方法是傳入self弱引用對(duì)象。
__weak typeof(self)weakSelf = self;
[config.userContentController addScriptMessageHandler:weakSelf name:@"doShare"];
測(cè)試結(jié)果:失敗,不走dealloc方法,內(nèi)存不釋放。
解決方式2:
在頁(yè)面出現(xiàn)時(shí)再添加監(jiān)聽(tīng),頁(yè)面消失時(shí)移除監(jiān)聽(tīng)。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_webView.configuration.userContentController addScriptMessageHandler:self name:@"doShare"];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_webView.configuration.userContentController removeScriptMessageHandlerForName:@"doShare"];
}
測(cè)試結(jié)果:成功,走dealloc方法,內(nèi)存釋放。
但是個(gè)人覺(jué)得這并不是一個(gè)完美的解決方案。因?yàn)檫@種解決方式不僅在頁(yè)面退出時(shí)會(huì)移除監(jiān)聽(tīng),在push進(jìn)新的頁(yè)面時(shí)監(jiān)聽(tīng)也會(huì)被移除,而此時(shí)頁(yè)面還在卻無(wú)法及時(shí)監(jiān)聽(tīng)到JS事件了。
解決方式3:
增加一個(gè)中間類(lèi)去弱引用WKWebView,斷開(kāi)循環(huán)引用。
WeakScriptMessageDelegate類(lèi)代碼如下:
.h
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
@property (nonatomic,weak)id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
NS_ASSUME_NONNULL_END
.m
#import "WeakScriptMessageDelegate.h"
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
- (void)dealloc {
}
@end
WKWebView創(chuàng)建
#import <WebKit/WebKit.h>
@property (nonatomic ,strong) WKWebView *webView;
- (WKWebView *)webView {
if (!_webView) {
//解決WKWebView與JS交互造成循環(huán)引用的問(wèn)題
WeakScriptMessageDelegate *weakScriptMessageDelegate = [[WeakScriptMessageDelegate alloc] initWithDelegate:self];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:weakScriptMessageDelegate name:@"doShare"];
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) configuration:config];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.allowsBackForwardNavigationGestures = YES;
}
return _webView;
}
- (void)dealloc {
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"doShare"];
}
測(cè)試結(jié)果:成功,走dealloc方法,內(nèi)存釋放。且控制器釋放時(shí)自動(dòng)解除監(jiān)聽(tīng)。