WKWebView 使用,代替UIWebView

很多app都会用到网页加载,UIWebView这个控件不可避免的会被使用到。这是一个谁用谁知道的控件,内存激增,加载慢等问题分分钟爆炸。所以,我们良心的苹果公司在iOS8之后推出了WKWebView来代替UIWebView!使用了WKWebView智斗,内存不爆表了,加载速度快了,一口气上五楼也不累了!!!

以下是一点WKWebView的用法:
首先当然是要初始化一个webView了

- (void)addWeb {
//首先当然是要初始化一个webView了
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 1)];
//设置代理
self.webView.navigationDelegate = self;
1、加载网页链接
//获取网页链接
NSURL *url = [NSURL URLWithString:self.detailUrl];
//将url转为request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//加载request请求
[self.webView loadRequest:request];
2、加载html格式内容
/* *
 * param1 : html文本
 * param2 : 网页链接,传nil就行
*/
[self.webView loadHTMLString:@"" baseURL:[NSURL URLWithString:@""]];

//将网页加到父类视图上!
[self.view addSubview:self.webView];
}

完成以上步骤之后还要实现代理方法哦,要不然你啥都看不到!

//网页加载完毕

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    //计算高度
    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        //获取页面高度,并重置webview的frame
        double height = [result doubleValue];
        CGRect frame = webView.frame;
        frame.size.height = height;
        webView.frame = frame;
    }];
}
//相应网页点击事件,处理网页内的点击事件
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
        //获取点击跳转的url地址
        NSString *urlString = [[navigationAction.request URL] absoluteString];
        //要跳转的页面
        ProductDetailViewController *ctrl = [[ProductDetailViewController alloc] init];
        ctrl.detailUrl = urlString;
        [self.navigationController pushViewController:ctrl animated:YES];
        //取消导航跳转
        decisionHandler(WKNavigationActionPolicyCancel);

    } else {
        //允许导航跳转
        decisionHandler(WKNavigationActionPolicyAllow);
    }
   
    
//    urlString = [urlString stringByRemovingPercentEncoding];
//    // 用://截取字符串
//    NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
//    if ([urlComps count]) {
//        // 获取协议头
//        NSString *protocolHead = [urlComps objectAtIndex:0];
//        NSLog(@"protocolHead=%@",protocolHead);
//    }
}

下面是一大波wkwebView的代理方法

// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
// 页面加载完毕后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
// 导航跳转错误时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 网页需要验证时调用
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
// 当网页内容进程被终止时调用
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;```

更高级的用法,还没有研究,后面研究了会继续写在这里!!!

你可能感兴趣的:(WKWebView 使用,代替UIWebView)