iOS WKWebView学习总结
Posted 预估计
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS WKWebView学习总结相关的知识,希望对你有一定的参考价值。
一、原生与H5页面交互方式
- 登陆后将token放入wkwebview的cookie中。以后wkwebview也可以同步原生app的登陆状态了。
以下代码 @"document.cookie = ‘UID=%@‘;document.cookie = ‘CLIENT=App‘;document.cookie = ‘TOKEN=%@‘" 根据自己项目的cookie格式传递。
- 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学习总结的主要内容,如果未能解决你的问题,请参考以下文章