码迷,mamicode.com
首页 > 移动开发 > 详细

iOS WKWebView学习总结

时间:2016-06-01 15:33:03      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:

一、原生与H5页面交互方式

  1. 登陆后将token放入wkwebview的cookie中。以后wkwebview也可以同步原生app的登陆状态了。

以下代码   @"document.cookie = ‘UID=%@‘;document.cookie = ‘CLIENT=App‘;document.cookie = ‘TOKEN=%@‘" 根据自己项目的cookie格式传递。

  1. 1 - (void)setNewCookieWithUserInfoToWebView{ 2 3 NSString *cookie = 4 [NSString stringWithFormat:@"document.cookie = ‘UID=%@‘;document.cookie = ‘CLIENT=App‘;document.cookie = ‘TOKEN=%@‘", 5 [USER_DEFAULT objectForKey:KUSERUID] == nil ? @"":[USER_DEFAULT objectForKey:KUSERUID],[USER_DEFAULT objectForKey:KUSERTOKEN] == nil ?@"":[USER_DEFAULT objectForKey:KUSERTOKEN]]; 6 WKUserScript *cookieScript = [[WKUserScript alloc] 7 initWithSource:cookie 8 injectionTime:WKUserScriptInjectionTimeAtDocumentStart 9 forMainFrameOnly:YES]; 10 [self.configuration.userContentController addUserScript:cookieScript]; 11 [self reloadFromOrigin]; 12 13 }

 

  2、审查wkwebview中的页面元素,提取wkwebview登陆页面中的登陆按钮所调用的JS代码(尽量向js开发人员要)

然后通过这句代码执行js:把参数传递到js中。让wkwebview执行js,wkwebview就登陆了。

以下代码  @"function clearCache(){localStorage.setItem(‘fundType‘,null);localStorage.setItem(‘fundTypeIndex‘,null);}clearCache();" 是纯js代码,ios程序可以直接使用。

1 NSString *js = @"function clearCache(){localStorage.setItem(‘fundType‘,null);localStorage.setItem(‘fundTypeIndex‘,null);}clearCache();";
2             [self.baseWebView evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) {
3             }];

  3、通过该方法,截取H5页面将要请求的页面地址,来做原生相应的操作

 1 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
 2     NSString *str = navigationAction.request.URL.absoluteString;//获取当前正在请求的url
 4     if ([str isEqualToString:@"https://m.cgjr.com/"] && self.viewController.tabBarController.selectedIndex != 0) {
 5         decisionHandler(WKNavigationActionPolicyCancel);
 6         [self.viewController.tabBarController setSelectedIndex:0];
 7     }
 8     if ([str isEqualToString:@"https://m.cgjr.com/site/login.html"] && self.viewController.tabBarController.selectedIndex == 0) {
 9         decisionHandler(WKNavigationActionPolicyCancel);
10         UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CGTZLogin" bundle:nil];
11         CGTZLoginViewController *navC = [sb instantiateViewControllerWithIdentifier:@"CGTZLoginViewController"];
12         navC.tabBarItem.title = @"登录";
13         [self.viewController presentViewController:navC animated:YES completion:^{
14         }];
15         [self.viewController.tabBarController setSelectedIndex:0];
16     }
17     if ([str isEqualToString:@"https://m.cgjr.com/site/login.html"] && self.viewController.tabBarController.selectedIndex == 1) {
18         decisionHandler(WKNavigationActionPolicyCancel);
19         UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CGTZLogin" bundle:nil];
20         CGTZLoginViewController *navC = [sb instantiateViewControllerWithIdentifier:@"CGTZLoginViewController"];
21         navC.tabBarItem.title = @"登录";
22         [self.viewController presentViewController:navC animated:YES completion:^{
23         }];
24         [self.viewController.tabBarController setSelectedIndex:1];
25     }
26     if ([str isEqualToString:@"https://m.cgjr.com/site/login.html"] && self.viewController.tabBarController.selectedIndex == 2) {
27         decisionHandler(WKNavigationActionPolicyCancel);
28         UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CGTZLogin" bundle:nil];
29         CGTZLoginViewController *navC = [sb instantiateViewControllerWithIdentifier:@"CGTZLoginViewController"];
30         navC.tabBarItem.title = @"登录";
31         [self.viewController presentViewController:navC animated:YES completion:^{
32         }];
33         [self.viewController.tabBarController setSelectedIndex:2];
34     }
35     decisionHandler(WKNavigationActionPolicyAllow);
36 }

 

iOS WKWebView学习总结

标签:

原文地址:http://www.cnblogs.com/yyyyyyyyqs/p/5549743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!