十九.WKWebViewシンプル使用+サードパーティのプログレスバー

5721 ワード

iOS 8以降、アップルは新しいフレームワークWebkitを発売し、UIWebViewに代わるコンポーネントWKWebViewを提供した.
WKWebViewの機能:
  • 性能は高くて、安定性は良くて、占有するメモリは比較的に小さい
  • JSインタラクション
  • をサポート
  • HTML 5の新機能
  • をサポート
  • 進捗バーを追加できます(これは私が選んだのはサードパーティで、最後に紹介します)
  • は組み込みジェスチャーをサポートし、
  • 60 fpsまでのスクロールリフレッシュ率および内蔵ジェスチャー1.作成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 ()
    {
        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
    {
    
    }