美文网首页2018技术笔记
2018笔记——WKWebView

2018笔记——WKWebView

作者: 满庭花醉三千客 | 来源:发表于2018-07-28 11:16 被阅读66次

项目中为了优化性能,将UIWebView替换为WKWebView,踩了一些坑:

1、WKWebVIew在iOS11之后才可以使用Xib构建,如果项目支持iOS11之前的系统,那么就不能直接用Xib构建了,但是替换还是要的,所以采取了如下方案:

构建一个空白的placeView,与原UIWebView一样的布局,用来占位的。然后我们再手动创建WKWebView,添加到空白的placeView中。

此外需要注意的点是:如果采用wkView.frame = placeView.bounds构建,需要在viewDidLayoutSubviews中更新一下frame。

2、不能加载http的链接:

屏幕快照 2018-07-09 下午3.16.35.png

然后再实现NavigationDelegate代理方法:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];

        completionHandler(NSURLSessionAuthChallengeUseCredential,card);

    }

}

3、web页面不能点击了,是因为web页面打开了了一个新的页面,需要如下设置

// 在请求开始加载之前,决定是否跳转

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler

{

    if (navigationAction.targetFrame == nil) {

        [webView loadRequest:navigationAction.request];

    }

    if (decisionHandler) {

        decisionHandler(WKNavigationActionPolicyAllow);

    }

}

4、获取到web的title

方案1:创建WKWebView时添加监听,在监听中获取到title:

- (WKWebView*)webView

{

    if (!_webView) {

        _webView = [[WKWebView alloc]initWithFrame:CGRectZero];

        _webView.navigationDelegate = self;

        [self.webBgView addSubview:_webView];

        [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

    }

    return _webView;

}

//WkWebView的 回调

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    if ([keyPath isEqualToString:@"title"]) {

        if (object == self.webView) {

            NSString *str = self.webView.title;

        } else {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }

    } else {

        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

}

方案2:使用java的函数

//加载完成

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation

{

    [_hud stopAnimating];

    [self.webView evaluateJavaScript:@"document.getElementById('title').innerHTML" completionHandler:^(id _Nullable string, NSError * _Nullable error) {

        if (!error && [string isKindOfClass:[NSString class]]) {

            NSString *str = string;

            _ttLabel.text = str&&str.length >0?str:[@"Travel Information" internationalString:@"CCP"];

            _ttLabel.hidden = NO;

        }

    }];

}

5、使用本地的html,该如何加载:

NSString *str1 = [[NSBundle mainBundle] pathForResource:@"travalAirport" ofType:@"html"];

NSURL *url = [NSURL fileURLWithPath:str1];

NSData *data = [NSData dataWithContentsOfFile:str1];

[self.webView loadData:data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:url];

加油~

相关文章

  • WKWebView的学习

    WKWebView的学习 重新整理前面的学习笔记WKWebView 原文网址 :http://www.jiansh...

  • 2018笔记——WKWebView

    项目中为了优化性能,将UIWebView替换为WKWebView,踩了一些坑: 1、WKWebVIew在iOS11...

  • iOS OC与JS交互(四)-- WebViewJavascri

    本篇笔记记录WebViewJavascriptBridge在WKWebView中的使用和原理分析 WebViewJ...

  • WKWebView笔记

    准备工作 · 导入框架 WebKit.framework · 导入头文件

  • WKWebView笔记

    前言 iOS8开始,苹果引入了新的web控件WKWebView替代UIWebView,WKWebView属于Web...

  • WKWebView 笔记

    WKWebView JS 交互 客户端中加入以下代码 可以监听JS->OC 当在页面中调用 以上JS代码后 在 d...

  • (转)WKWebView学习笔记

    上一篇整理了下wkwebview的一些属性及API,然后又发现有一个写的比我完整的wkwebview的学习笔记,怕...

  • iOS新一轮网址收藏

    创建于:2018年10月31日 1、文章:iOS (一) - UIWebView 与 WKWebView . 基本...

  • UITableView含有UIWebView或WKWebView

    2018年7月14日一.tableView上webView(UIWebView和 WKWebView)高度运算不正...

  • WKWebView学习笔记

    一、简介 webView是我们日常开发中不可缺少的一个组件,通常我们都是使用UIWebView来实现的,不过大多数...

网友评论

    本文标题:2018笔记——WKWebView

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