WKWebView在遵循WKNavigationDelegate代理后,会有一个- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;方法,监听特定的URL
//网页交互
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *lowStr = navigationAction.request.URL.absoluteString;
// NSString *urlStr = [NSString strurl]
PTTLog(@"跳转网址为%@",lowStr);
if ([lowStr containsString:@"qla"]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}else{
decisionHandler(WKNavigationActionPolicyAllow);
}
}
然后在AppDelegate文件中添加-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;方法,并解析URL,来实现对应功能。
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
if (!url) {
return NO;
}
NSString *urlString=[url absoluteString];
if (![urlString containsString:@"?"]) {
[_tabViewController setSelectedIndex:0];
return YES;
}
//获取问号的位置,问号后是参数列表
NSRange range = [urlString rangeOfString:@"?"];
//获取参数列表
NSString *propertys = [urlString substringFromIndex:(int)(range.location+1)];
//进行字符串的拆分,通过&来拆分,把每个参数分开
NSArray *subArray = [propertys componentsSeparatedByString:@"&"];
//把subArray转换为字典
//tempDic中存放一个URL中转换的键值对
NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:4];
for (int j = 0 ; j < subArray.count; j++)
{
//在通过=拆分键和值
NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="];
if ([dicArray[0] isEqualToString:@"title"]) {
//urf8转码
NSString *str = dicArray[1];
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[tempDic setObject:utf forKey:dicArray[0]];
}else{
// PTTLog(@"再把每个参数通过=号进行拆分:n%@", dicArray);
//给字典加入元素
[tempDic setObject:dicArray[1] forKey:dicArray[0]];
}
}
UITabBarController *tabbarVC = (UITabBarController *)self.window.rootViewController;
if ([tabbarVC isKindOfClass:[QLStartADViewController class]]) {
return NO;
}
UINavigationController *navigationVC = (UINavigationController *)tabbarVC.selectedViewController;
if (((NSNumber *)[tempDic objectForKey:@"action"]).intValue == 101) {
//跳转到首页
[tabbarVC setSelectedIndex:0];
[navigationVC popToRootViewControllerAnimated:YES];
}else if (((NSNumber *)[tempDic objectForKey:@"action"]).intValue == 102){
//快捷入金
if (!readloginState) {
[navigationVC pushViewController:[[USStartUpController alloc]init] animated:YES];
}else{
if (readisOpenAcount) {
TransferMoneyViewController *vc = [[TransferMoneyViewController alloc]init];
[navigationVC pushViewController:vc animated:YES];
vc.activity = [tempDic objectForKey:@"activity"];
vc.tradeRoute = [tempDic objectForKey:@"tradeRoute"];
}else{
OpenAccountInfoController *openVc = [[OpenAccountInfoController alloc]init];
[navigationVC pushViewController:openVc animated:YES];
}
}
}
return YES;
}