标签:
一、原生与H5页面交互方式
以下代码 @"document.cookie = ‘UID=%@‘;document.cookie = ‘CLIENT=App‘;document.cookie = ‘TOKEN=%@‘" 根据自己项目的cookie格式传递。
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 }
标签:
原文地址:http://www.cnblogs.com/yyyyyyyyqs/p/5549743.html