WKWebView 的使用简介

news/2024/7/5 1:50:23

1. navigationDelegate

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  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

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  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 执行脚本方法

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  1. - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(idNSError *))completionHandler;  

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  1. completionHandler 拥有两个参数,一个是返回错误,一个可以返回执行脚本后的返回值  

4. WKWebView 的Cookie问题

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

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

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  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

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  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];  

[objc] view plaincopy
print?在CODE上查看代码片派生到我的代码片
  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]; 

http://lihuaxi.xjx100.cn/news/235412.html

相关文章

Codeforces Educational round 58

Ediv2 58 随手AK.jpgD 裸的虚树&#xff0c;在这里就不写了 E 傻逼贪心&#xff1f;这个题过的比$B$都多.jpg #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> #include <queue> #includ…

基于Idea从零搭建一个最简单的vue项目

一、需要了解的基本知识 node.js Node.js是一个Javascript运行环境(runtime)&#xff0c;发布于2009年5月&#xff0c;由Ryan Dahl开发&#xff0c;实质是对Chrome V8引擎进行了封装。Node.js对一些特殊用例进行优化&#xff0c;提供替代的API&#xff0c;使得V8在非浏览器环境…

高德地图POI搜索,附近地图搜索,类似附近的人搜索

效果图&#xff1a; 首先导入道德地图的SDK&#xff0c;导入步骤不在这里介绍 2&#xff1a;包含头文件&#xff1a; [objc] view plaincopy #import <AMapSearchKit/AMapSearchAPI.h> 3&#xff1a;代码 [javascript] view plaincopy property(nonatomic,strong)AMap…

20位程序员关于求职的疑问,以及我给出的参考答案

作者&#xff1a;陆小凤首发&#xff1a;公众号【程序员江湖】阅读本文大概需要 6 分钟。前几天发了一条朋友圈对于求职小伙伴们提出的问题&#xff0c;我进行了收集整理&#xff0c;统一反馈。也许这20个问题也是你们遇到的问题&#xff0c;所以趁着年前赶紧把它发出来。以下2…

开博啦!

2007年10月10日&#xff0c;终于在博客园安家啦&#xff0c;希望以后和大家分享一些我的知识和经验&#xff0c;交到志同道合的朋友。我的奋斗目标是成为博客园里的高手之一。 希望多多棒场。 转载于:https://www.cnblogs.com/sandy/archive/2007/10/10/920110.html

Swift - AppDelegate.swift类中默认方法的介绍

项目创建后&#xff0c;AppDelegate类中默认带有如下几个方法&#xff0c;具体功能如下&#xff1a; 1&#xff0c;应用程序第一次运行时执行这个方法只有在App第一次运行的时候被执行过一次&#xff0c;每次App从后台激活时都不会再执行该方法。&#xff08;注&#xff1a;所有…

【转载】gdi+ 内存泄漏

【转载】http://issf.blog.163.com/blog/static/1941290822009111894413472/ 最近用GDI实现了几个自定义控件&#xff0c;但是发现存在内存泄露问题 BOOL CGdiplusBugDlg::OnEraseBkgnd(CDC* pDC) {Image* pImage Image::FromFile(L"E:\\bac.bmp");Graphics g(pDC-&…

三、数据类型

一、有序无序可变不可变#1、有序&#xff1a;但凡有索引的数据都是有序的 #2、可变不可变 #可变类型&#xff1a;在值变了的情况下&#xff0c;id不变&#xff0c;证明在改原值 #不可变类型&#xff1a;在值变了的情况下&#xff0c;id也跟着变&#xff0c;证明不是在改原值二、…