主要是设置info.plist下的URL Schemes
我们开发的过程中遇到这样一个需求,如果手机下了app,就打开并且跳转到指定页面,否则跳转到app store.
首先在info.plist下的URL Schemes下 新增一个Schemes
然后在浏览器下 打开链接
todolist://
在iOS8下 因该是可以直接打开app的,在iOS9下会询问是否打开某某app
如果需要传参数,就在后面传参数
todolist://?action=inGroup¶0=g123456¶1=u123456
在AppDelegate里
iOS8
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSLog(@"openURL : %@", url);
NSLog(@"sourceApplication : %@", sourceApplication);
// 在这里我是自己解析的参数 据说可以用[]url query]取得参数 我没试过
NSString *strUrl = [NSString stringWithFormat:@"%@", url];
NSString *queryStr = [[strUrl componentsSeparatedByString:@"://?"] lastObject];
NSArray *queryParameters = [queryStr componentsSeparatedByString:@"&"];
NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
for (NSString *strCondition in queryParameters) {
NSArray *keyValueArr = [strCondition componentsSeparatedByString:@"="];
[paramDict setObject:[keyValueArr lastObject] forKey:[keyValueArr firstObject]];
}
NSLog(@"paramDict : %@", paramDict);
// 跳转到指定页面并且传para0
ZNTabBarController *zntabBar = (ZNTabBarController *)self.window.rootViewController;
UINavigationController * nav = (UINavigationController *)zntabBar.selectedViewController;
CircleMainController *circleMain = [[CircleMainController alloc] init];
circleMain.groupsId = [paramDict objectForKey:@"para0"];
circleMain.hidesBottomBarWhenPushed = YES;
[nav pushViewController:circleMain animated:YES];
return YES;
}
iOS9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
NSLog(@"%@", options);
NSString *strUrl = [NSString stringWithFormat:@"%@", url];
NSString *queryStr = [[strUrl componentsSeparatedByString:@"://?"] lastObject];
NSArray *queryParameters = [queryStr componentsSeparatedByString:@"&"];
NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
for (NSString *strCondition in queryParameters) {
NSArray *keyValueArr = [strCondition componentsSeparatedByString:@"="];
[paramDict setObject:[keyValueArr lastObject] forKey:[keyValueArr firstObject]];
}
NSLog(@"paramDict : %@", paramDict);
// 跳转到指定页面
ZNTabBarController *zntabBar = (ZNTabBarController *)self.window.rootViewController;
UINavigationController * nav = (UINavigationController *)zntabBar.selectedViewController;
CircleMainController *circleMain = [[CircleMainController alloc] init];
circleMain.groupsId = [paramDict objectForKey:@"para0"];
circleMain.hidesBottomBarWhenPushed = YES;
[nav pushViewController:circleMain animated:YES];
return YES;
}
再说下app间的跳转
NSURL *url = [NSURL URLWithString:@"todolist://com.hello/?a=1&b=2&c=333"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message" message:@"地址有误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
如果可以打开,就打开app 否则alert
但是目前有个问题一直没解决
屏幕快照 2016-04-27 下午2.34.02.png就是js代码中的window.location("todolist://com.hello/?a=1&b=2&c=333") 这句 在qq的webview中 执行的话 好像跳不到app中(就是加入圈子按钮,在iOS8下 safari打开可以跳转 在iOS9 或直接点击按钮 不跳转 ) 不知道是什么原因,如果有知道的 麻烦请告知我
网友评论