在 iOS 开发的过程中,我们经常会遇到需要从一个应用程序 A 跳转到另一个应用程序 B 的场景。这就需要我们掌握 iOS 应用程序之间的相互跳转知识。
下面来看看我们在开发过程中遇到的应用场景。
那么我们如何实现应用间的相互跳转呢?先来看下原理。
在 iOS 中打开一个应用程序只需要拿到这个应用程序的协议头即可,所以我们只需配置应用程序的协议头即可。
假设有应用 A 和应用 B 两个应用,现在需要从应用 A 跳转到应用 B 中。
具体怎么做呢,下面一步步来教你,先来个简单点的:从应用 A 跳转到应用 B。
如果是 iOS9 之前的模拟器或是真机,那么在相同的模拟器中先后运行 App-B、App-A,点击按钮,就可以实现跳转了。
- - (IBAction) jumpToAppB: (id) sender {
- // 1.获取应用程序App-B的URL Scheme
- NSURL * appBUrl = [NSURL URLWithString: @"AppB://"];
- // 2.判断手机中是否安装了对应程序
- if ([[UIApplication sharedApplication] canOpenURL: appBUrl]) {
- // 3. 打开应用程序App-B
- [[UIApplication sharedApplication] openURL: appBUrl];
- } else {
- NSLog(@"没有安装");
- }
- }
具体做法就是在 App-A 的 Info 文件中,添加 LSApplicationQueriesSchemes 数组,然后添加键值为 AppB 的字符串。
添加 LSApplicationQueriesSchemes 数组,然后添加键值为 AppB 的字符串
添加白名单之后在相同的模拟器中先后运行 App-B、App-A,点击按钮,就可以实现跳转了。
具体效果如下图所示。
App-A 跳转到 App-B
下边学习以下从应用 A 跳转到应用 B 的特定界面。
很多时候,我们做应用程序之间的跳转并不只是跳转到其他程序就可以了,而是要跳转到其他程序的特定页面上。比如我们在浏览网页时,会有分享到微信朋友圈或是分享给微信朋友,这就需要跳转到微信朋友圈界面或是微信朋友选择界面。
具体如何做呢?
搭建两个页面 `Page1` 和 `Page2`
设置 Page1ViewController 的标识符
添加两个跳转页面按钮
- - (IBAction) jumpToAppBPage1: (id) sender {
- // 1.获取应用程序App-B的Page1页面的URL
- NSURL * appBUrl = [NSURL URLWithString: @"AppB://Page1"];
- // 2.判断手机中是否安装了对应程序
- if ([[UIApplication sharedApplication] canOpenURL: appBUrl]) {
- // 3. 打开应用程序App-B的Page1页面
- [[UIApplication sharedApplication] openURL: appBUrl];
- } else {
- NSLog(@"没有安装");
- }
- } - (IBAction) jumpToAppBPage2: (id) sender {
- // 1.获取应用程序App-B的Page2页面的URL
- NSURL * appBUrl = [NSURL URLWithString: @"AppB://Page2"];
- // 2.判断手机中是否安装了对应程序
- if ([[UIApplication sharedApplication] canOpenURL: appBUrl]) {
- // 3. 打开应用程序App-B的Page2页面
- [[UIApplication sharedApplication] openURL: appBUrl];
- } else {
- NSLog(@"没有安装");
- }
- }
3. 在应用 App-B 中通过 AppDelegate 监听跳转,进行判断,执行不同页面的跳转
- - (BOOL) application: (UIApplication * ) application openURL: (NSURL * ) url sourceApplication: (NSString * ) sourceApplication annotation: (id) annotation {
- // 1.获取导航栏控制器
- UINavigationController * rootNav = (UINavigationController * ) self.window.rootViewController;
- // 2.获得主控制器
- ViewController * mainVc = [rootNav.childViewControllers firstObject];
- // 3.每次跳转前必须是在跟控制器(细节)
- [rootNav popToRootViewControllerAnimated: NO];
- // 4.根据字符串关键字来跳转到不同页面
- if ([url.absoluteString containsString: @"Page1"]) { // 跳转到应用App-B的Page1页面
- // 根据segue标示进行跳转
- [mainVc performSegueWithIdentifier: @"homeToPage1"sender: nil];
- } else if ([url.absoluteString containsString: @"Page2"]) { // 跳转到应用App-B的Page2页面
- // 根据segue标示进行跳转
- [mainVc performSegueWithIdentifier: @"homeToPage2"sender: nil];
- }
- return YES;
- }
具体效果如下:
App-A 跳转到 App-B 的特定界面
。即:AppB://Page1?AppA。
- 协议头://应用B的URL Schemes?应用A的URL Schemes
方法获取将要跳转的页面控制器。
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
添加 Page1 和 Page2 的页面控制器 Page1ViewController 和 Page2ViewController
、
- @"AppB://Page1?AppA"
- @"AppB://Page2?AppA"
,并在 App-B 中通过 AppDelegate 中保存完整的 URL。
- @property (nonatomic, copy) NSString *urlString;
,用于接受、截取出跳转回的应用(即 App-A)的 URL Schemes,执行跳转。
- @property (nonatomic, copy) NSString *urlString;
方法。
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- - (void) prepareForSegue: (UIStoryboardSegue * ) segue sender: (id) sender {
- if ([segue.identifier isEqualToString: @"homeToPage1"]) {
- // 获得将要跳转的界面Page1的控制器
- Page1ViewController * Page1Vc = segue.destinationViewController;
- // 保存完整的App-A的URL给跳转界面Page1
- Page1Vc.urlString = self.urlString;
- } else if ([segue.identifier isEqualToString: @"homeToPage2"]) {
- // 获得将要跳转的界面Page2的控制器
- Page2ViewController * Page2Vc = segue.destinationViewController;
- // 保存完整的App-A的URL给跳转界面Page1
- Page2Vc.urlString = self.urlString;
- }
- }
- Page2ViewController.m
- - (IBAction)page1BackToAppA:(id)sender {
- // 1.拿到对应应用程序的URL Scheme
- NSString *urlSchemeString = [[self.urlString componentsSeparatedByString:@"?"] lastObject];
- NSString *urlString = [urlSchemeString stringByAppendingString:@"://"];
- // 2.获取对应应用程序的URL
- NSURL *url = [NSURL URLWithString:urlString];
- // 3.判断是否可以打开
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- }
- }
- - (IBAction)page2BackToAppA:(id)sender {
- // 1.拿到对应应用程序的URL Scheme
- NSString *urlSchemeString = [[self.urlString componentsSeparatedByString:@"?"] lastObject];
- NSString *urlString = [urlSchemeString stringByAppendingString:@"://"];
- // 2.获取对应应用程序的URL
- NSURL *url = [NSURL URLWithString:urlString];
- // 3.判断是否可以打开
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- }
- }
具体效果如下:
App-B 跳转回 App-A
也可以在 Safari 中,键入使用定制模式的 URL(myapp://),确认是否启动 B 应用,就可以从 Safari 中打开注册的 B 应用。
详情请看 App Programming Guide for iOS
来源: http://www.bubuko.com/infodetail-2448398.html