美文网首页程序员
十九.WKWebView简单使用+第三方的进度条

十九.WKWebView简单使用+第三方的进度条

作者: 择一城终老_蜗牛 | 来源:发表于2017-05-23 10:54 被阅读0次

    iOS8以后,苹果推出了新框架Webkit,提供了替换UIWebView的组件WKWebView。

    WKWebView的特性:

    • 性能高,稳定性好,占用的内存比较小
    • 支持JS交互
    • 支持HTML5 新特性
    • 可以添加进度条(这个我选择的还是第三方,最后介绍)
    • 支持内建手势,
    • 高达60fps的滚动刷新率以及内置手势
      一.创建
      1.导入Wbkit这个类库
      2.创建个WebView的对象(基本与uiwebview一致)
        WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
        [self.view addSubview:webView];
    

    3.代理方法:

    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    

    pragma mark - WKNavigationDelegate

    1. WKNavigationDelegate来追踪加载过程
    // 页面开始加载时调用
    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
    // 当内容开始返回时调用
    - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
    // 页面加载完成之后调用
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
    // 页面加载失败时调用
    - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
    
    2.WKNavigtionDelegate页面跳转相关
    // 接收到服务器跳转请求之后再执行
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;
    // 在收到响应后,决定是否跳转
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
               NSLog(@"%@",navigationResponse.response.URL.absoluteString);
        //允许跳转
              decisionHandler(WKNavigationResponsePolicyAllow);
        //不允许跳转
              //decisionHandler(WKNavigationResponsePolicyCancel);
    }
    // 在发送请求之前,决定是否跳转
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
    

    pragma mark - WKUIDelegate

    // 创建一个新的WebView
    - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
        return [[WKWebView alloc]init];
    }
    // 输入框
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{
        completionHandler(@"http");
    }
    // 确认框
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
        completionHandler(YES);
    }
    // 警告框
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
        NSLog(@"%@",message);
        completionHandler();
    }
    

    关于介绍一些交互用法可参考:http://www.cocoachina.com/ios/20161121/18142.html

    第三方的进度条 我使用的是NJKWebViewProgress

    (当然如果用进度条,我还是使用的是uivebview的控件)
    连接:https://github.com/ninjinkun/NJKWebViewProgress

    #import "detailTeachVC.h"
    #import "NJKWebViewProgress.h"
    #import "NJKWebViewProgressView.h"
    @interface detailTeachVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
    {
        NJKWebViewProgressView *_progressView;
        NJKWebViewProgress *_progressProxy;
    }
    @property(nonatomic,strong)UIWebView *webview;
    @end
    
    @implementation detailTeachVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor=[UIColor whiteColor];
        _webview=[[UIWebView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:_webview];
        
        _progressProxy = [[NJKWebViewProgress alloc] init];
        _progressProxy.webViewProxyDelegate = self;
        _progressProxy.progressDelegate = self;
    
        _webview.delegate=_progressProxy;//#这个代理设置的不是self#
    
        CGFloat progressBarHeight = 2.f;
        CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
        CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
        _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
        _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
        
        [self initData];
    }
    -(void)initData{
        
        NSString *url=[NSString stringWithFormat:@"https://www.baidu.com/"];
        [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        
    }
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.navigationController.navigationBar addSubview:_progressView];
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        
        // Remove progress view
        // because UINavigationBar is shared with other ViewControllers
        [_progressView removeFromSuperview];
    }
    #pragma mark - NJKWebViewProgressDelegate
    -(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
    {
        [_progressView setProgress:progress animated:YES];
        self.title = [_webview stringByEvaluatingJavaScriptFromString:@"document.title"];
    }
    #pragma mark - UIWebViewDelegate
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
    
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
    
    }
    

    相关文章

      网友评论

        本文标题:十九.WKWebView简单使用+第三方的进度条

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