iOS 一个app跳转另一个app并实现通信(如A跳到B并打开B中指定页面)
2016-06-14 17:13
936 查看
功能实现:A跳到B并打开B中指定页面
步骤:
1.首先创建两个项目(项目A,项目B),在项目B中的info.plist文件中添加URL Types,如下图所示:
其中URL idenifier是项目B的bundle id ,URL Schemes 中添加一个命令前缀,我这里使用“projectB”,这个名字可以自己取,运行一下项目B。
2.在项目A中添加跳转代码
1
1
这里的URL的命令前缀必须和之前自己定义的一致,我把这行代码加到了一个button的点击方法里,现在点击button就可以跳到项目B了。
3.现在说下app之间跳转的通信,其实跟传值差不多。项目A中第二个button的点击方法添加代码
1
1
4 . 项目B中在appDelegate中添加一个NSURL的属性,实现一个代理方法接收从项目A传过来的URL
1
2
3
4
5
1
2
3
4
5
然后在B中第一个界面加上代码
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];
}
}
步骤:
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];
}
}
相关文章推荐
- 摇一摇工具类
- 关于Android自定义Dialog细节问题
- Android逆向之旅---反编译利器Apktool和Jadx源码分析以及错误纠正
- VSTO 右击菜单 系统类型分类 (Application.CommandBars)
- RecyclerView的简单使用(1)
- Android Studio Git 版本控制 on Windows10--从系统配置到具体使用
- android5.0 GPS详解
- Android自定义LinearLayout实现左右侧滑菜单,完美兼容ListView、ScrollView、ViewPager等滑动控件
- Android Studio 第一次配置及其使用
- Android中ServiceManager的功能
- WebView的简单用法
- iOS开发Xcode6及以上版本中使用PDF格式矢量图标资源
- application windows are expected to have a root view controller
- Xcode常用快捷键
- iOS NSDate时间换算
- 关于AndroidStudio升级到2.0的一些问题汇总
- 去除android或者iOS系统默认的一些样式总结
- android 更改api版本
- iOS 持续集成- 使用xctool实现自动化打包
- Android.mk