本篇文章小编给大家分享一下Html5跳转到APP指定页面实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.设置urlschemes
urlschemes尽量设一个唯一的字符串,例如可以设为:iOS+公司英文名+ 项目工程名
比如设为iOSTencentTest,在浏览器中输入地址iOSTencentTest://即可跳转到我的app
2.跳转到指定页面
在使用iOSTencentTest://打开app会调用AppDelegate的代理方法
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
跳转指定页面在该方法中操作
iOSTencentTest://后面是可以添加参数的,例如iOSTencentTest://goodsDetails?id=xxxxx
goodsDetails可直接通过url.host获取
id=xxxxx 参数可直接通过url.query获取
可以根据自身需求去设置不同的host和参数。
h5那边只需要执行:
window.location.href = 'iOSTencentTest://goodsDetails?id=xxxxx'
附:
//获取Window当前显示的ViewController - (UIViewController*)currentViewController{ //获得当前活动窗口的根视图 UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController; while (1) { //根据不同的页面切换方式,逐步取得最上层的viewController if ([vc isKindOfClass:[UITabBarController class]]) { vc = ((UITabBarController*)vc).selectedViewController; } if ([vc isKindOfClass:[UINavigationController class]]) { vc = ((UINavigationController*)vc).visibleViewController; } if (vc.presentedViewController) { vc = vc.presentedViewController; }else{ break; } } return vc; } //NSString类别方法 //通过url.query获取参数字符 再分成字典 -(NSMutableDictionary *)getURLParameters { if (!self.length) { return nil; } NSMutableDictionary *params = [NSMutableDictionary dictionary]; if ([self containsString:@"&"]) { NSArray *urlComponents = [self componentsSeparatedByString:@"&"]; for(NSString *keyValuePair in urlComponents) { //生成key/value NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="]; NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding]; NSString*value = [pairComponents.lastObject stringByRemovingPercentEncoding]; //key不能为nil if(key==nil|| value ==nil) continue; id existValue = [params valueForKey:key]; if(existValue !=nil) { //已存在的值,生成数组。 if([existValue isKindOfClass:[NSArray class]]) { //已存在的值生成数组 NSMutableArray*items = [NSMutableArray arrayWithArray:existValue]; [items addObject:value]; [params setValue:items forKey:key]; }else{ //非数组 [params setValue:@[existValue,value]forKey:key]; } }else{ //设置值 [params setValue:value forKey:key]; } } }else { //单个参数生成key/value NSArray *pairComponents = [self componentsSeparatedByString:@"="]; if(pairComponents.count==1) { return nil; } //分隔值 NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding]; NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding]; //key不能为nil if(key ==nil|| value ==nil)return nil; //设置值 [params setValue:value forKey:key]; } return params; }
模拟极限越野
模拟极限越野开着大巴在各种奇葩路况里送乘客。山路弯道多得让人
模拟火车
模拟火车让你当回真正的火车司机,得先学会看信号灯、操作控制杆
可口的披萨美味的披萨
可口的披萨美味的披萨带您体验经营一家披萨店的乐趣。游戏画风温
定制虚拟偶像最新版
定制虚拟偶像最新版是一款超级经典有趣的日本模拟装扮类型的手游
病娇模拟器手机版正版(yandere simulator)
病娇模拟器最新版手游是一款萌妹二次元模拟游戏,玩家在游戏中可