苹果手机中的APP都有一个沙盒,APP就是一个单独个体,信息独立,相互是不能够进行通讯的.可是iOS的APP能够注册本身的URL Scheme,URL Scheme是为方便App之间相互调用而设计的.咱们能够经过系统的openURL来打开该APP,并能够传递一些参数,URL Scheme必须能惟一表示一个APP,若是你设置的URL Scheme和逼得APP的URL Scheme冲突时,你的APP不必定会被启动起来.由于当你的APP在安装的时候,系统里面已经注册了你的URL Scheme.通常状况下,是会调用先安装的APP.可是iOS的系统APP的URL Scheme确定是最高的,因此咱们定义URL Scheme的时候,尽可能避开系统APP已经定义过的URL Scheme数组
URL Scheme的命名应该是只能纯英文字符,而不能含有下划线或者数字浏览器
在 TARGETS -> Info -> URL Types 点击添加 bash
在info.plist中右击,选中 微信
URL Identifier是自定义的 URL scheme 的名字,通常采用反转域名的方法保证该名字的惟一性,好比 com.DemoB.www,不过在iOS中打开一个应用程序只须要拿到这个应用程序的协议头(URL Scheme)便可,因此咱们只需配置应用程序的协议头便可。一个应用是能够有多个URL Schemes的app
这里建立了两个应用:DemoA 和DemoB,DemoB注册了Scheme为"DemoBScheme",下面来实现DemoA→DemoB的跳转ui
#pragma mark - DemoA -> DemoB
- (IBAction)jumpToDemoB:(id)sender {
NSString *urlString = @"DemoBScheme://";//没有参数
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else {
[self showMessage:@"没有该应用"];
}
}
复制代码
分别运行两个应用,而后在DemoA中点击相应的按钮跳转到DemoB,会发现并不能跳转,由于在iOS9之后,若是使用canOpenURL:方法,改方法所涉及的URL Scheme必须在Info.plist中将它们列为白名单,不然不能使用url
在info.plist中添加LSApplicationQueriesSchemes字段,该字段对应的是数组类型,而后添加键值DemoBScheme(DemoB的Scheme) spa
有的时候咱们须要跳转到某个应用的特定页面,好比分享的时候须要分享到朋友圈页面设计
#pragma mark - 跳转到pageOne页面
- (IBAction)jumpToPageOne:(id)sender {
// DemoBScheme:是DemoB应用的scheme page1是与DemoB约定好的跳转到PageOne页面的标识符 ?是分割符(固然也能够用其余符号做分割),在DemoB中经过分隔符来截取DemoA的scheme, DemoAScheme是本身的scheme,用来从DemoB跳转回来
NSString *urlString = @"DemoBScheme://page1?DemoAScheme";
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else {
[self showMessage:@"没有该应用"];
}
}
复制代码
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
// 1.获取导航栏控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.得到主控制器
UIViewController *mainVc = [rootNav.childViewControllers firstObject];
// 3.每次跳转前必须是在跟控制器(细节)
[rootNav popToRootViewControllerAnimated:NO];
if ([url.absoluteString containsString:@"page1"]) {//与DemoA约定好的字符
PageOneViewController *page = [[PageOneViewController alloc] init];
page.urlString = url.absoluteString;
[mainVc.navigationController pushViewController:page animated:YES];
}
else if ([url.absoluteString containsString:@"page2"]) {
PageOneViewController *page = [[PageOneViewController alloc] init];
page.urlString = url.absoluteString;
[mainVc.navigationController pushViewController:page animated:YES];
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"
message:[NSString stringWithFormat:@"scheme - %@,\n host -- %@,\n query -- %@",url.scheme,url.host,url.query]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
NSLog(@"query -- %@", url.query);
[alertView show];
}
return YES;
}
复制代码
这样就实现了DemoA跳转到DemoB中某个特定页面3d
iOS 9.0以前应在- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 方法中设置
上面讲了从DemoA跳转到DemoB,有的时候在跳转完以后须要再跳回到本来的app,好比分享完成以后会让选择返回到原来的app仍是留在微信
#pragma mark - 跳转到pageOne页面
- (IBAction)jumpToPageOne:(id)sender {
//咱们想要从应用B再跳转回应用A,那么在跳转到应用B的时候,还应将应用A的URL Schemes传递过来。这样咱们才能判断应该跳转回哪一个应用程序。
// 这样咱们指定一个传递URL的规则:协议头://应用B的URL Schemes?应用A的URL Schemes。即:AppB://Page1?AppA。
//是DemoB应用的scheme page1是与DemoB约定好的跳转到PageOne页面的标识符 ?是分割符(固然也能够用其余符号做分割),在DemoB中经过分隔符来截取DemoA的scheme, DemoAScheme是本身的scheme,用来从DemoB跳转回来
NSString *urlString = @"DemoBScheme://page1?DemoAScheme";// DemoBScheme:
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else {
[self showMessage:@"没有该应用"];
}
}
复制代码
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
// 1.获取导航栏控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.得到主控制器
UIViewController *mainVc = [rootNav.childViewControllers firstObject];
// 3.每次跳转前必须是在跟控制器(细节)
[rootNav popToRootViewControllerAnimated:NO];
if ([url.absoluteString containsString:@"page1"]) {//与DemoA约定好的字符
PageOneViewController *page = [[PageOneViewController alloc] init];
page.urlString = url.absoluteString;
[mainVc.navigationController pushViewController:page animated:YES];
}
else if ([url.absoluteString containsString:@"page2"]) {
PageOneViewController *page = [[PageOneViewController alloc] init];
page.urlString = url.absoluteString;
[mainVc.navigationController pushViewController:page animated:YES];
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"
message:[NSString stringWithFormat:@"scheme - %@,\n host -- %@,\n query -- %@",url.scheme,url.host,url.query]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
NSLog(@"query -- %@", url.query);
[alertView show];
}
return YES;
}
复制代码
#pragma mark - 返回DemoA
- (IBAction)backDemoA:(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 options:@{} completionHandler:nil];
}
}
复制代码
. 有时候咱们跳转另外一个APP的时候须要传递一些参数,让另外一个APP根据咱们传递的参数作出相应的行为
传参格式:
"DemoBScheme://?name=lwy&phone=110" 和平时的get请求传参是同样的
#pragma mark - DemoA -> DemoB
- (IBAction)jumpToDemoB:(id)sender {
// NSString *urlString = @"DemoBScheme://";//没有参数
NSString *urlString = @"DemoBScheme://?name=lwy&phone=110";//后面拼接参数
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}
else {
[self showMessage:@"没有该应用"];
}
}
复制代码
NSString *query = url.query;
复制代码
网页打开app也是根据app的协议头(URL scheme)来区分打开的是哪个app的,咱们直接在浏览器上复制粘贴咱们的url scheme ,系统会自动弹框提醒是否打开本应用,跟DemoA跳转到DemoB的跳转是同样的, 固然网页传参跟app跳转传参都是同样的格式