iOS wkwebview懒加载中遇到的问题

1、懒加载传值问题

2、kvo监听影响 (报错:An instance 0x7fd9cb793f30 of class WKWebView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7fd9cdca68b0>)

 

1、这是我遇到的问题,也许是个例,就算狗血了点吧

需求: 当前界面(mainVC)响应点击事件,传值给webviewController(webVC)其中包含网址,此时如果在webVC中对wkwebview进行懒加载并挂代理:

问题:在mainVC中调用webVC传值时,wkwebview的懒加载就开始了,,,此时如果传值中的网址为空值,webVC中的代理立马报错崩溃,没办法服务器就是会给空的,只能取消懒加载

 

//懒加载

-(WKWebView*)wkWebView{
    //self.debugDescription
    WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init];
    //允许视频播放
    Configuration.allowsAirPlayForMediaPlayback = YES;
    // 允许在线播放
    Configuration.allowsInlineMediaPlayback = YES;
    // 允许可以与网页交互,选择视图
    Configuration.selectionGranularity = YES;
    // web内容处理池
    Configuration.processPool = [[WKProcessPool alloc] init];
    //自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
    WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
    // 添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
    [UserContentController addScriptMessageHandler:self name:@"yurong"];
    // 是否支持记忆读取
    Configuration.suppressesIncrementalRendering = YES;
    // 允许用户更改网页的设置
    Configuration.userContentController = UserContentController;
    _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, self.navigationBar.bottom, ScreenWidth, ScreenHeight-self.navigationBar.bottom) configuration:Configuration];
    _wkWebView.backgroundColor = whiteCo;
    // 设置代理
    _wkWebView.navigationDelegate = self;
    _wkWebView.UIDelegate = self;
    
    return _wkWebView;
}

 

 

2、

//注意,观察的移除

-(void)dealloc{

    

    if ([self isViewLoaded]) {

        [self.wkWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];

    }

    

    // if you have set either WKWebView delegate also set these to nil here

    [self.wkWebView setNavigationDelegate:nil];

    [self.wkWebView setUIDelegate:nil];

}

转载于:https://www.cnblogs.com/xujiahui/p/7232625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值