zoukankan      html  css  js  c++  java
  • WKWebView

    WKWebView相对于UIWebView强大了很多,内存的消耗相对少了,所提供的接口也丰富了。

    现在谈一谈WKWebView的基本使用

    1. navigationDelegate

    1. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { // 类似UIWebView的 -webViewDidStartLoad:  
    2.     NSLog(@"didStartProvisionalNavigation");  
    3.     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;  
    4. }  
    5.   
    6. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {  
    7.     NSLog(@"didCommitNavigation");  
    8. }  
    9.   
    10. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { // 类似 UIWebView 的 -webViewDidFinishLoad:  
    11.     NSLog(@"didFinishNavigation");  
    12.     [self resetControl];  
    13.     if (webView.title.length > 0) {  
    14.         self.title = webView.title;  
    15.     }  
    16.   
    17. }  
    18. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {  
    19.     // 类似 UIWebView 的- webView:didFailLoadWithError:  
    20.   
    21.     NSLog(@"didFailProvisionalNavigation");  
    22.       
    23. }  
    24. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {  
    25.       
    26.     decisionHandler(WKNavigationResponsePolicyAllow);  
    27. }  
    28.   
    29.   
    30. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {  
    31.     // 类似 UIWebView 的 -webView: shouldStartLoadWithRequest: navigationType:  
    32.   
    33.     NSLog(@"4.%@",navigationAction.request);  
    34.   
    35.       
    36.     NSString *url = [navigationAction.request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    37.   
    38.       
    39.       
    40.     decisionHandler(WKNavigationActionPolicyAllow);  
    41.   
    42. }  
    43. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {  
    44.       
    45. }  


    2 UIDelegate

    1. - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {  
    2.     // 接口的作用是打开新窗口委托  
    3.     [self createNewWebViewWithURL:webView.URL.absoluteString config:configuration];  
    4.       
    5.     return currentSubView.webView;  
    6. }  
    7.   
    8. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler  
    9. {    // js 里面的alert实现,如果不实现,网页的alert函数无效  
    10.     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message  
    11.                                                                              message:nil  
    12.                                                                       preferredStyle:UIAlertControllerStyleAlert];  
    13.     [alertController addAction:[UIAlertAction actionWithTitle:@"确定"  
    14.                                                         style:UIAlertActionStyleCancel  
    15.                                                       handler:^(UIAlertAction *action) {  
    16.                                                           completionHandler();  
    17.                                                       }]];  
    18.       
    19.     [self presentViewController:alertController animated:YES completion:^{}];  
    20.       
    21. }  
    22.   
    23.   
    24. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {  
    25.     //  js 里面的alert实现,如果不实现,网页的alert函数无效  ,   
    26.   
    27.     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message  
    28.                                                                              message:nil  
    29.                                                                       preferredStyle:UIAlertControllerStyleAlert];  
    30.     [alertController addAction:[UIAlertAction actionWithTitle:@"确定"  
    31.                                                         style:UIAlertActionStyleDefault  
    32.                                                       handler:^(UIAlertAction *action) {  
    33.                                                           completionHandler(YES);  
    34.                                                       }]];  
    35.     [alertController addAction:[UIAlertAction actionWithTitle:@"取消"  
    36.                                                         style:UIAlertActionStyleCancel  
    37.                                                       handler:^(UIAlertAction *action){  
    38.                                                           completionHandler(NO);  
    39.                                                       }]];  
    40.       
    41.     [self presentViewController:alertController animated:YES completion:^{}];  
    42.       
    43. }  
    44.   
    45. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler {  
    46.       
    47.     completionHandler(@"Client Not handler");  
    48.       
    49. }  



    3. WKWebView 执行脚本方法

    1. - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler;  
    1. completionHandler 拥有两个参数,一个是返回错误,一个可以返回执行脚本后的返回值  

    4. WKWebView 的Cookie问题

    UIWebView 中会自动保存Cookie,如果登录了一次,下次再次进入的时候,会记住登录状态

    而在WKWebView中,并不会这样,WKWebView在初始化的时候有一个方法

    1. - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration  

    通过这个方法,设置 configuration 让WKWebView知道登录状态,configuration 可以通过已有的Cookie进行设置,也可以通过保存上一次的configuration进行设置

    参考 stackoverflow上回答:http://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303

    1. WKWebView * webView = /*set up your webView*/  
    2. NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/index.html"]];  
    3. [request addValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;" forHTTPHeaderField:@"Cookie"];  
    4. // use stringWithFormat: in the above line to inject your values programmatically  
    5. [webView loadRequest:request];  
      1. WKUserContentController* userContentController = WKUserContentController.new;  
      2. WKUserScript * cookieScript = [[WKUserScript alloc]   
      3.     initWithSource: @"document.cookie = 'TeskCookieKey1=TeskCookieValue1';document.cookie = 'TeskCookieKey2=TeskCookieValue2';"  
      4.     injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];  
      5. // again, use stringWithFormat: in the above line to inject your values programmatically  
      6. [userContentController addUserScript:cookieScript];  
      7. WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;  
      8. webViewConfig.userContentController = userContentController;  
      9. WKWebView * webView = [[WKWebView alloc] initWithFrame:CGRectMake(/*set your values*/) configuration:webViewConfig];  

    相关链接:

    自己动手打造基于 WKWebView 的混合开发框架(一)——WKWebView 上手

    iOS8 WebKit库之——WKWebView篇

    使用iOS8 WKWebView的浏览器模块,脉冲动画层

    【iOS】WKWebView的新特性与使用

    WKWebView 的使用简介

    UIWebView、WKWebView使用详解及性能分析

  • 相关阅读:
    2017.1.10学习笔记
    v-model双向绑定
    指令之v-bind
    v-for列表渲染
    事件修饰符
    v-on
    指令v-text v-html
    vue起步
    Swoole HTTPServer
    SwooleTCP
  • 原文地址:https://www.cnblogs.com/On1Key/p/5539870.html
Copyright © 2011-2022 走看看