app之间的跳转,进入二级界面

时间:2023-03-09 00:18:40
app之间的跳转,进入二级界面

功能实现:A跳到B并打开B中指定页面.http://blog.csdn.net/dollyyang/article/details/50325307

点击页面判断是否安装app并打开,否则跳转app store的方法

步骤:

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

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

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

这里的URL的命令前缀必须和之前自己定义的一致,我把这行代码加到了一个button的点击方法里,现在点击button就可以跳到项目B了。 
app之间的跳转,进入二级界面app之间的跳转,进入二级界面

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

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

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

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
self.url = url;
return YES;
}
  • 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

app之间的跳转,进入二级界面 
app之间的跳转,进入二级界面 
简而言之,就是根据从A中传过来的URL打开项目B后进行一些自定义操作