iOS 应用之间相互跳转,即从一个app打开另一个app,实现两个app之间的交互和通信,苹果有一个类UIApplication来管理,主要用到以下两个方法
- (BOOL)canOpenURL:(NSURL *)url
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion
下面通过实例来说明:
1.在SchemesA项目中,配置 URL Schemes选项如图;
2.在SchemesB项目中的info.plist中配置LSApplicationQueriesSchemes选项,Type为数组,值为SchemesA中的URL Schemes即testA(iOS9以后,iOS9之前不用配置)
3.在SchemesB项目 ViewController.m 中,通过URL Schemes,完成跳转功能,详见下面代码(完成跳转到指定页面功能)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/*
补充知识:
NSURL *url = [NSURL URLWithString:@"http://192.168.0.1/path?page=100"];
NSLog(@"scheme(协议):%@",url.scheme);//结果:scheme(协议):http
NSLog(@"host(域名):%@",url.host);//结果:host(域名):192.168.0.1
NSLog(@"path(路径):%@",url.path);//结果:path(路径):/path
NSLog(@"query(参数):%@",url.query);//结果:query(参数):page=100
*/
// 1.通过testA://?page跳转到SchemesA控制器的主页
// NSURL *url = [NSURL URLWithString:@"testA://"];
// 2.通过 testA://page?testB 跳转到SchemesA控制器的指定页面并把当前项目的 URL Scheme传递过去,通过query
NSURL *url = [NSURL URLWithString:@"testA://page?testB"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else
{
NSLog(@"没有安装应用SchemesA");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.在SchemesA项目 AppDelegate.m 中,实现如下方法,详情见下面代码以及注释,并传递url.query到FirstViewController中
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
NSLog(@"url -> %@",url.query);
//1.获取导航栏控制器
UINavigationController *navc = (UINavigationController *)self.window.rootViewController;
//2.每次跳转前必须是在跟控制器(注意事项)
[navc popToRootViewControllerAnimated:NO];
//3.获得主控制器
ViewController *vc = [navc.childViewControllers lastObject];
if ([url.query isEqualToString:@"testB"])
{
//跳转到指定页面
FirstViewController *firstVc = [[FirstViewController alloc] init];
firstVc.queryString = url.query;
[vc.navigationController pushViewController:firstVc animated:YES];
}
return YES;
}
至此整个过程已经完成。
扩展功能:跳转后再返回到应用中
5.在SchemesB项目中,首先完成第一个步骤,在SchemesA项目中,完成第二个步骤, 在SchemesB项目中的FirstViewController.h 中声明属性
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic,strong) NSString *queryString;
@end
6.在SchemesA项目中 FirstViewController.m 中,通过 query 跳转到自己的应用中
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
NSLog(@"self.queryString --> %@",self.queryString);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//跳转到testA控制器的Url
NSString *queryUrlString = [self.queryString stringByAppendingString:@"://"];
NSURL *url = [NSURL URLWithString:queryUrlString];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else
{
NSLog(@"没有安装应用SchemesB");
}
}