zoukankan      html  css  js  c++  java
  • WKWebView的使用

    一直以来只用过UIWebView,看别人源码项目用到了WKWebView,这里记录一下,毕竟iOS8建议使用WKWebView了.

    - (instancetype)initWithUrl:(NSString *)url {
        if (self = [super init]) {
            self.url = url;
        }
        return self;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSURL *URL = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [self.webView loadRequest:[NSURLRequest requestWithURL:URL]];
    }
    
    - (WKWebView *)webView {
        if (!_webView) {
            WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
            [self.view addSubview:wkWebView];
            _webView = wkWebView;
            wkWebView.UIDelegate = self;
            wkWebView.navigationDelegate = self;
            wkWebView.opaque = NO;
            wkWebView.backgroundColor = self.view.backgroundColor;
        }
        return _webView;
    }
    
    #pragma mark - WKNavigationDelegate
    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
        
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
        
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
        
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
     
        // 允许跳转
        decisionHandler(WKNavigationResponsePolicyAllow); 
    }
    
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
      
        // 允许跳转
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    
    - (void)pop {
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            
            if ([self.webView canGoBack]) {
                [self.webView goBack];
            } else  {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        
    }
    

      

  • 相关阅读:
    C语言集锦(一) C代码生成图片:BMP、PNG和JPEG
    Win32 OpenGL标准例子
    Tcc学习笔记(一) 开篇
    C语言集锦(二) 图像显示 Windows和Linux
    矢量图和Word:EPS,PDF,EMF和SVG
    firefox浏览器无法显示bootstrap图标问题总结
    现代字体栈
    jquery 插件大全
    meta 详解,html5 meta 标签日常设置
    工厂方法模式Factory Method(Java实现)
  • 原文地址:https://www.cnblogs.com/pengsi/p/5985058.html
Copyright © 2011-2022 走看看