美文网首页
iOS使用WKWebView不调用dealloc

iOS使用WKWebView不调用dealloc

作者: 路有点颠簸 | 来源:发表于2020-04-29 18:04 被阅读0次

    1、使用了定时器,NSTimer没有销毁

    - (void)viewDidDisappear:(BOOL)animated{
        [super viewDidDisappear:animated];
        if (self.timer) {
            [_timer invalidate];
            _timer = nil;
        }
    }
    

    2、delegate属性使用weak、asign修饰符

    3、block使用注意使用弱引用,避免强引用

    #define WeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self;
    #define StrongSelf(strongSelf)  __strong __typeof(&*self)strongSelf = weakSelf;
    

    4、注册与JS交互时把self传给了MessageHandler

    [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"getStorage"];
    

    所以需要在页面关闭时移除js交互对象,可以在viewDidDisappear中移除

    [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"getStorage"];
    

    5、我遇到的小坑,特殊情况

    因为是创建了一个manager继承NSObject,专门来管理webView,并且在需要使用网页的控制器中把self传入了这个manager,当时我在manager中是这样声明对象的

    @property (nonatomic, Strong) BaseViewController *controller;
    

    很明显这和上面说到的是一个道理,controller强引用manager,manager强引用controller,所以循环引用了,这些需要改成weak申明

    @property (nonatomic, weak) BaseViewController *controller;
    

    总结:dealloc不执行需要一步一步排查,我也花了小半天,以上是我遇到的情况,欢迎补充~

    相关文章

      网友评论

          本文标题:iOS使用WKWebView不调用dealloc

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