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

转载于:https://www.cnblogs.com/er-dai-ma-nong/p/5584724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值