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

}

posted @ 2017-07-25 10:25  徐家汇  阅读(866)  评论(0编辑  收藏  举报