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].ViewController;
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];
}
pdf转html
}
}else {
//单个参数⽣成key/value
NSArray *pairComponents = [self componentsSeparatedByString:@"="];
unt==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;
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。