iOS App之间跳转

1.先来看看效果,这里做了三个功能

  1. 从MyApp跳转到YourApp
  2. 从MyApp跳转到YourApp的指定页面
  3. 从YourApp使用跳转url的方式跳回MyApp

2.实现app之间的跳转需要注意两方面

  1. 实现跳转的代码
  2. 配置url及url白名单

3首先来讲url和白名单的设置

  1. 创建两个工程分别叫MyApp和YourApp
  2. 设置MyApp的url,设置的方法有两种,都是等效的,如下图
  3. 设置YourApp的url
  4. 设置MyApp的白名单
    1. 在info.plist中添加LSApplicationQueriesSchemes的数组,其中有含有一个string类型的数据,即为你要跳转的另外一个App的url,我们要从MyApp跳转到YourApp,我们把YourApp的url设置为your,所以这里要写入YourApp的url“your”
  5. 设置YourApp的白名单,我写的demo中有添加用url跳转回MyApp的功能所以要设置YourApp的白名单,如果没有的话是不用设置的,系统自带的有跳转回初始APP的方法
  6. 注意不要把白名单的key写错了

4.实现跳转的代码

  1. MyApp中的代码
    1. - (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(@"打开应用失败");
          }
      }
  2.  YourApp中的代码
    1. 写在AppDelegate.m中
    2. //如果是通过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;
      }
    3. 想要跳转到MyApp的时候
    4. - (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

原文地址:https://www.cnblogs.com/czq1989/p/5318936.html