Html5自动跳转到APP特定网页页面的完成

2021-02-22 12:22 jianzhan

1.设定urlschemes

urlschemes尽可能设1个唯1的标识符串,比如能够设为: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:@"&"]) {
        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;
}

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。