iOS 一个app跳转另外一个app并实现通讯(如A跳到B并打开B中指定页面)

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

步骤:app

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

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

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

这里的URL的命令前缀必须和以前本身定义的一致,我把这行代码加到了一个button的点击方法里,如今点击button就能够跳到项目B了。
这里写图片描述这里写图片描述lua

3.如今说下app之间跳转的通讯,其实跟传值差很少。项目A中第二个button的点击方法添加代码url

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

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

-(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后进行一些自定义操做code

 

 

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];
        }
    }component

相关文章
相关标签/搜索