zoukankan      html  css  js  c++  java
  • iOS 一个app跳转另一个app并实现通信(如A跳到B并打开B中指定页面)

    功能实现:A跳到B并打开B中指定页面

    步骤:

    1.首先创建两个项目(项目A,项目B),在项目B中的info.plist文件中添加URL Types,如下图所示:这里写图片描述其中URL idenifier是项目B的bundle id ,URL Schemes 中添加一个命令前缀,我这里使用“projectB”,这个名字可以自己取,运行一下项目B。

    2.在项目A中添加跳转代码

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"projectB://"]];
    • 1
    • 1

    这里的URL的命令前缀必须和之前自己定义的一致,我把这行代码加到了一个button的点击方法里,现在点击button就可以跳到项目B了。
    这里写图片描述这里写图片描述

    3.现在说下app之间跳转的通信,其实跟传值差不多。项目A中第二个button的点击方法添加代码

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"projectB://openBSecondPage"]];
    • 1
    • 1

    4 . 项目B中在appDelegate中添加一个NSURL的属性,实现一个代理方法接收从项目A传过来的URL

    -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
    {
        self.url = url;
        return YES;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    然后在B中第一个界面加上代码

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSURL * url = ((AppDelegate *)[UIApplication sharedApplication].delegate).url;
       ;
        if(url){
            //显示一下从A获取的url,url = projectB://openBSecondPage,host = openBSecondPage
            self.label.text = [NSString stringWithFormat:@"url = %@,host = %@",[url absoluteString],[url host]];
            //根据传过来的url的host进行一些操作
            if ([[url host]isEqualToString:@"openBSecondPage"]) {
                //跳转到第二个界面
                [self performSegueWithIdentifier:@"second" sender:nil];
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这里写图片描述
    这里写图片描述
    简而言之,就是根据从A中传过来的URL打开项目B后进行一些自定义操作

    b的具体解析过程为:    NSURL * url = ((AppDelegate *)[UIApplication sharedApplication].delegate).url;
        ;
        if(url){

            NSArray *arr = [url.host componentsSeparatedByString:@"&"];
            
            NSLog(@"%@",arr);
            
            UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 300, 300)];
            
            lab.text = [NSString stringWithFormat:@"%@",arr];
            
            lab.numberOfLines = 0;
            
            [self.view addSubview:lab];
            
            //根据传过来的url的host进行一些操作
            if ([arr.lastObject isEqualToString:@"openurl=openBSecondPage"]) {
                //跳转到第二个界面
                secondViewController *sec = [[secondViewController alloc]init];
                
                [self.navigationController pushViewController:sec animated:YES];
            }
        }

  • 相关阅读:
    Unobtrusive Ajax
    Asp.Net Web API 2(入门)第一课
    c# in depth之泛型的实现
    ASP.NET MVC 單元測試系列
    菜单栏
    【C++ 中文手册】即将完成
    AspNet MVC3中过滤器 + 实例
    虚拟机安装Linux中常见异常及解决办法
    webbrowser打开新窗口事件+=
    Java Bad version
  • 原文地址:https://www.cnblogs.com/er-dai-ma-nong/p/5584724.html
Copyright © 2011-2022 走看看