美文网首页
iOS开发 WKWebview 和JS 交互时造成当前控制器不

iOS开发 WKWebview 和JS 交互时造成当前控制器不

作者: 有O梦想的咸鱼 | 来源:发表于2019-02-22 16:47 被阅读9次

    在WkWebView与JavaScript交互中,经常会在原生中注入JS 消息,如下:

    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    [userContentController addScriptMessageHandler:self name:@"handleGoMall"];
    然后在WKScriptMessageHandler的回调方法中:
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
      QYLog(@"message.name:%@", message.name);
      QYLog(@"message.body:%@",message.body);
      // 根据name做想做的操作
      if ([message.name isEqualToString:@"handleGoMall"]) {//跳转购买滤芯    
    }
    }
    

    这样做可以达到预期效果,但是当你退出当前控制器的时候会发现当前控制器没有被销毁也就是说不走dealloc方法,这是因为在向JS中注入handler的时候强引用了self,最终导致内存泄漏,
    解决方法:
    创建一个新类:QYWeakScriptMessageHandlerDelegate

    @interface QYWeakScriptMessageHandlerDelegate : NSObject<WKScriptMessageHandler>
    
    @property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
    - (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
    
    @end
    
    @implementation QYWeakScriptMessageHandlerDelegate
    - (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
        if (self = [super init]) {
            _scriptDelegate = scriptDelegate;
        }
        return self;
    }
    
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
        [self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
    }
    

    使用:
    把:

     [userContentController addScriptMessageHandler:self name:@"handleGoMall"];
    替换成:
     [userContentController addScriptMessageHandler:[[QYWeakScriptMessageHandlerDelegate alloc] initWithDelegate:self] name:@"handleGoMall"];
    

    最后在dealloc中释放:

    - (void)dealloc {
        [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"handleGoMall"];
    }
    

    这样退出当前控制器时,当前控制器就会销毁了。

    相关文章

      网友评论

          本文标题:iOS开发 WKWebview 和JS 交互时造成当前控制器不

          本文链接:https://www.haomeiwen.com/subject/tumeyqtx.html