1.先来看看效果,这里做了三个功能
2.实现app之间的跳转需要注意两方面
3首先来讲url和白名单的设置
4.实现跳转的代码
- (IBAction)gotoYoueApp:(UIButton *)sender { // 1.获取application对象 UIApplication *app = [UIApplication sharedApplication]; // 2.创建要打开的应用程序的URL NSURL *url = [NSURL URLWithString:@"your://aaa"]; // 3.判断是否可以打开另一个应用 if ([app canOpenURL:url]) { // 能,就打开 [app openURL:url]; }else{ NSLog(@"打开应用失败"); } }
//如果是通过URL打开的本应用,则下面的方法会被执行 -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { //获取window中加载的根视图,也就是那个导航 UINavigationController *navi = (UINavigationController *)self.window.rootViewController; //为了触发push第二个界面的segue,需要拿到 //左边灰色背景的那个控制器的引用 //而灰色的那个控制器是navi的根视图控制器 //vc代表灰色的那个界面 ViewController *vc = (ViewController *)navi.topViewController; //1.获取请求的URL地址 NSString *urlString = [url absoluteString]; //2.判断地址中包含的信息为bbb则打开第二个页面 if ([urlString hasPrefix:@"your://bbb"]) { [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil]; } return YES; } //新的用于响应从URL跳转过来的方法 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { //获取window中加载的根视图,也就是那个导航 UINavigationController *navi = (UINavigationController *)self.window.rootViewController; //为了触发push第二个界面的segue,需要拿到 //左边灰色背景的那个控制器的引用 //而灰色的那个控制器是navi的根视图控制器 //vc代表灰色的那个界面 ViewController *vc = (ViewController *)navi.topViewController; //1.获取请求的URL地址 NSString *urlString = [url absoluteString]; //2.判断地址中包含的信息为bbb则打开第二个页面 if ([urlString hasPrefix:@"your://bbb"]) { [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil]; } return YES; }
- (IBAction)goBackMyApp:(id)sender { UIApplication *app = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:@"my://"]; if ([app canOpenURL:url]) { [app openURL:url]; }else{ NSLog(@"跳回到myapp失败"); } }
5.demo:https://github.com/TigerCui/MyAppJumpToYourApp.git