美文网首页
iOS App之间跳转 跳转 指定页面

iOS App之间跳转 跳转 指定页面

作者: 一滴矿泉水 | 来源:发表于2020-12-17 10:51 被阅读0次
    一、先来看看效果,这里做了三个功能

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


    1790850-f25857bae5ca696f.gif
    二、实现app之间的跳转需要注意两方面

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

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

    1、创建两个工程分别叫MyApp和YourApp
    2、设置MyApp的url schemes,设置的方法有两种,都是等效的,如下图


    1790850-dbd62a54bf2d0601.png 1790850-dee5882bb0cff5b0.png

    3、设置YourApp的url


    image

    4、设置MyApp的白名单
    在info.plist中添加LSApplicationQueriesSchemes的数组,其中有含有一个string类型的数据,即为你要跳转的另外一个App的url schemes,我们要从MyApp跳转到YourApp,我们把YourApp的url schemes设置为your,所以这里要写入YourApp的url schemes “your”


    image

    5、设置YourApp的白名单,我写的demo中有添加用url schemes跳转回MyApp的功能所以要设置YourApp的白名单,如果没有的话是不用设置的,系统自带的有跳转回初始APP的方法


    image

    6、注意不要把白名单的key写错了

    四、实现跳转的代码

    1、MyApp中的代码

    - (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 options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:nil];
    } else {
        NSLog(@"打开应用失败");
    }
    

    }
    2、YourApp中的代码
    写在AppDelegate.m中

    //如果是通过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则打开第二个页面(bbb 可以将bbb替换为自己需要打开的 控制器名称 ,app间需要交互的参数也可拼接在此 url 中)
      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则打开第二个页面(bbb 可以将bbb替换为自己需要打开的 控制器名称 ,app间需要交互的参数也可拼接在此 url 中)
      if ([urlString hasPrefix:@"your://bbb"])
      {
          [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil];
      }
      return YES;
    }
    

    3、想要跳转到MyApp的时候

    - (IBAction)goBackMyApp:(id)sender {
        UIApplication *app = [UIApplication sharedApplication];
        NSURL *url = [NSURL URLWithString:@"my://"];
        if ([app canOpenURL:url]) {
         [app openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES}     completionHandler:nil];
          } else {
            NSLog(@"打开应用失败");
          }
    }
    

    目前 原作者demo 中的打开app的 openURL方法 可更换为文章中新的系统Api方法。
    demo:https://github.com/TigerCui/iOSDemo/tree/master/MyAppJumpToYourApp


    文章部分内容借鉴链接:(https://www.jianshu.com/p/0495852bc428)

    文章持续更新中、希望对各位有所帮助、有问题可留言 大家共同学习.

    相关文章

      网友评论

          本文标题:iOS App之间跳转 跳转 指定页面

          本文链接:https://www.haomeiwen.com/subject/fcpagktx.html