ios wkwebview

3970 ワード

紹介する
WWDC 2014はios 8を発売し、webkit対応のWKWebViewはuiwebviewよりも強く、メモリ、ロード速度が優れている.
  • は性能、安定性、機能の面で
  • を大きく向上させた.
  • JavaScriptのNitroライブラリのロードと使用を許可する(UIWebViewで制限)
  • は、より多くのHTML 5プロパティ
  • をサポートしています.
  • 最大60 fpsのスクロールリフレッシュ率および内蔵ジェスチャー
  • UIWebViewDelegateとUIWebViewを再構成する14クラスと3つのプロトコル
  • 環境
    Mac OS X 10.10.1
    Xcode 6.1.1
    iOS 8.1
    初期化
    #import 
    
    //      
    - (instancetype)initWithFrame:(CGRect)frame;
    
    //    webview     ,     
    - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
    
    //    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    [self.view addSubview:webView];
    

    WKWebViewのエージェントメソッド
    WKWebViewのエージェントメソッド
    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;
    
    //    
    //               
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;
    //       ,      
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
    //        ,      
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
    

    実行順序:
    webView:decidePolicyForNavigationAction:decisionHandler(            )
    webView:didStartProvisionalNavigation
    webView:decidePolicyForNavigationResponse:decisionHandler(       error  )
    webView:didCommitNavigation
    webView:didFinishNavigation
    

    2. WKUIDelegate
    //       WebView
    - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
    
    //   、   、   
    /**
     *  web            
     *
     *  @param webView                 webview
     *  @param message                  
     *  @param frame                
     *  @param completionHandler        
     */
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler;
    

    3. WKScriptMessageHandler
    Appとwebのインタラクションを向上させ、受信したJSスクリプトをOCまたはSwiftオブジェクトに変換
    //  web             
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
    

    4.WKWebViewロードJS
    //      js  
    NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=320;};window.alert('  ' + count + '  ');"; 
    
    //   JS      WKUserScript   
    WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; 
    //      WKUserScript  ,   WKWebViewConfiguration 
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; 
    [config.userContentController addUserScript:script]; 
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; 
    [_webView loadHTMLString:@"![](http://upload-images.jianshu.io/upload_images/832398-a96d086fbf5dc89c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)" baseURL:nil];
    [self.view addSubview:_webView];