Html5跳转到APP指定页面的实现

发布时间:2022-04-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Html5跳转到APP指定页面的实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.设置urlschemes

urlschemes尽量设一个唯一的字符串,例如可以设为:iOS+公司英文名+ 项目工程名
比如我的设为iOSTencenttest,在浏览器中输入地址iOSTencentTest://即可跳转到我的app

2.跳转到指定页面

在使用iOSTencentTest://打开app会调用AppDelegate的代理方法

-(BOOL)application:(UIApplication *)app oPEnURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)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:@"&amp;"]) {
        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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本宝典。

脚本宝典总结

以上是脚本宝典为你收集整理的Html5跳转到APP指定页面的实现全部内容,希望文章能够帮你解决Html5跳转到APP指定页面的实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:跳转跳转