WkWebviewのいくつかの使用

5870 ワード

プロジェクトにH 5をロードするには、UIWebViewではなくWkWebviewを使用しました.前者は後者より多くのメリットがあるからです.
  • より多くのHTML 5をサポートする機能
  • 公式に発表された60 fpsまでのスクロールリフレッシュ率と内蔵ジェスチャー
  • はUIWebViewDelegateとUIWebViewを14種類と3つのプロトコルに分割し、以前は多くの不便な機能が実現されていた.https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/ObjC_classic/index.html
  • Safari同じJavaScriptエンジン
  • は、より少ないメモリを消費しています.また、WkWebviewはiOS 8以上のシステムをサポートしています.

  • 基本的な使い方
    WKWebViewにはdelegate,WKUIdelegate,WKNavigationDelegteの2つがあります.WKNavigationDelegateは主にいくつかのジャンプ、ロード処理操作を処理し、WKUIdelegateは主にJSスクリプト、確認ボックス、警告ボックスなどを処理する.そのため、WKNavigationDelegateはもっとよく使われています.
    #pragma mark - 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{
     
        NSLog(@"%@",navigationResponse.response.URL.absoluteString);
        //    
        decisionHandler(WKNavigationResponsePolicyAllow);
        //     
        //decisionHandler(WKNavigationResponsePolicyCancel);
    }
    //        ,      
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
     
         NSLog(@"%@",navigationAction.request.URL.absoluteString);
        //    
        decisionHandler(WKNavigationActionPolicyAllow);
        //     
        //decisionHandler(WKNavigationActionPolicyCancel);
    }
    #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();
    }
    

    OCとJSのインタラクション
    WKWebviewは、JavaScriptCoreまたはwebJavaScriptBridgeを使用することなく、jsインタラクションを実現するAPIを提供します.WKUSerContentControlを使用してjs nativeインタラクションを実現します.簡単に言えば、約束した方法を登録してから呼び出します.
    @interface ViewController (){
        WKWebView * webView;
        WKUserContentController* userContentController;
    }
    @end
    @implementation ViewController
    #pragma mark - lifeCircle
    - (void)viewDidLoad {
        [super viewDidLoad];
        //    
        WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
        userContentController =[[WKUserContentController alloc]init];
        configuration.userContentController = userContentController;
        webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) configuration:configuration];
        //    
        WKDelegateController * delegateController = [[WKDelegateController alloc]init];
        delegateController.delegate = self;
     
        [userContentController addScriptMessageHandler:delegateController  name:@"sayhello"];
     
        [self.view addSubview:webView];
        [webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
        }];
        webView.UIDelegate = self;
        webView.navigationDelegate = self;
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com"]]];
    }
    - (void)dealloc{
        //      ,           remove 。
        [userContentController removeScriptMessageHandlerForName:@"sayhello"];
    }
    #pragma mark - WKScriptMessageHandler
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
        NSLog(@"name:%@\\\
    body:%@\\\
    frameInfo:%@\\\
    ",message.name,message.body,message.frameInfo); } @end WKDelegateController : #import #import @protocol WKDelegate - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message; @end @interface WKDelegateController : UIViewController @property (weak , nonatomic) id delegate; @end .m : #import "WKDelegateController.h" @interface WKDelegateController () @end @implementation WKDelegateController - (void)viewDidLoad { [super viewDidLoad]; } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{ if ([self.delegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) { [self.delegate userContentController:userContentController didReceiveScriptMessage:message]; } } @end