声明:
本文章是参考:船长_的文章:http://www.jianshu.com/p/732c5e1720d0 所写,请大家方便时移步船长_ 的博客为他点赞。
谢谢!
iOS 应用之间的跳转,是通过设置"URL Scheme"来实现的。
简单来说:假设现在有两个应用A与B,我们分别设置了A和B的"URL Scheme",作为这两个App的标识,并通过在A和B的程序中一些设置,来达到应用跳转的目的。
具体的操作流程如下:
1、首先设置A与B的"URL Scheme":
2、将A和B的"URL Scheme"添加到对方的白名单中:
苹果公司在 iOS9之后 引入白名单概念。
在iOS9中,如果使用 canOpenURL: 方法,该方法所涉及到的 URL scheme 必须在"Info.plist"中将它们列为白名单,否则不能使用。(白名单上限是50个)。
PS:在 iOS9 之前,你可以使用
[[UIApplication sharedApplication] canOpenURL:url]
监测用户手机里到底装没装某些应用,来给用户提示。
但是也有一些别有用心的App ,这些App有一张常用 App 的"URL scheme"列表,他们会多次调用
[[UIApplication sharedApplication] canOpenURL:url]
遍历该表,来监测用户手机都装了什么 App 。
比如:"你"装了叫“大姨妈”的App,他就可以知道"你"是女性,他就可以只推给"你"女性用品的广告。
这是属于侵犯用户隐私的行为,可能就是导致苹果推出白名单的原因。
3、界面、演示及代码:
3.1、界面:
首先看一下A应用的界面:
B应用的界面为:
3.2、演示图如下:
3.3、代码:
A:
在A应用的
Appdelegate.m
中,代码如下:
#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] init]];
self.window.rootViewController = rootNav;
[self.window makeKeyAndVisible];
return YES;
}
// 过期
- (BOOL)application:(UIApplication *)application handleOpenURL: (NSURL *)url
{
NSLog(@"application.1 == %@",application);
NSLog(@"url.1 == %@",url);
return YES;
}
// 过期
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"application.2 == %@",application);
NSLog(@"url.2 == %@",url);
NSLog(@"sourceApplication.2 == %@",sourceApplication);
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
// 1.获取通过哪一个URL打开我的应用程序
NSString *urlStr = url.absoluteString;
// 2.取出window的根控制器
UINavigationController *rootNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
// 首先回到根控制器
[rootNav popToRootViewControllerAnimated:NO];
// 3.取出MainViewController,使用主要控制器就可以跳转到另外两个控制器
ViewController *rootVc = rootNav.childViewControllers.firstObject;
if ([urlStr containsString:@"confirm"])
{
// 确定界面
SecondViewController *viewCon = [[SecondViewController alloc] init];
[rootVc.navigationController pushViewController:viewCon animated:YES];
}
else if ([urlStr containsString:@"cancel"])
{
// 取消界面
ThirdViewController *viewCon = [[ThirdViewController alloc] init];
[rootVc.navigationController pushViewController:viewCon animated:YES];
}
return YES;
}
@end
在A应用的ViewController.m
中,代码如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"项目A";
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"跳转B应用" forState:UIControlStateNormal];
[self.view addSubview:button];
button.frame = CGRectMake(0, 0, 100, 20);
button.center = CGPointMake(self.view.frame.size.width/2, 100);
[button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonTwo setTitle:@"跳转B界面二" forState:UIControlStateNormal];
[self.view addSubview:buttonTwo];
buttonTwo.frame = CGRectMake(0, 0, 100, 20);
buttonTwo.center = CGPointMake(self.view.frame.size.width/2, 160);
[buttonTwo addTarget:self action:@selector(jumpViewTwo:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonThree = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonThree setTitle:@"跳转B界面三" forState:UIControlStateNormal];
[self.view addSubview:buttonThree];
buttonThree.frame = CGRectMake(0, 0, 100, 20);
buttonThree.center = CGPointMake(self.view.frame.size.width/2, 220);
[buttonThree addTarget:self action:@selector(jumpViewThree:) forControlEvents:UIControlEventTouchUpInside];
}
// 直接跳转B应用
- (void)jump:(UIButton *)button
{
NSURL *url = [NSURL URLWithString:@"ObjectB://"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}
// 跳转B应用界面二
- (void)jumpViewTwo:(UIButton *)button
{
// 注意:这里将应用A的URL当做参数传给应用B
NSURL *url = [NSURL URLWithString:@"ObjectB://session?ObjectA"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}
// 跳转B应用界面三
- (void)jumpViewThree:(UIButton *)button
{
// 注意:这里将应用A的URL当做参数传给应用B
NSURL *url = [NSURL URLWithString:@"ObjectB://timeLine?ObjectA"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}
@end
B:
应用B在Appdelagate.m
中,代码如下:
#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] init]];
self.window.rootViewController = rootNav;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL: (NSURL *)url
{
NSLog(@"application.1 == %@",application);
NSLog(@"url.1 == %@",url);
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"application.2 == %@",application);
NSLog(@"url.2 == %@",url);
NSLog(@"sourceApplication.2 == %@",sourceApplication);
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
// 1.获取通过哪一个URL打开我的应用程序
NSString *urlStr = url.absoluteString;
// 2.取出window的根控制器
UINavigationController *rootNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
// 首先回到根控制器
[rootNav popToRootViewControllerAnimated:NO];
// 3.取出MainViewController,使用主要控制器就可以跳转到另外两个控制器
ViewController *rootVc = rootNav.childViewControllers.firstObject;
if ([urlStr containsString:@"session"])
{
// 界面二
SecondViewController *viewCon = [[SecondViewController alloc] init];
viewCon.urlPath =urlStr;
[rootVc.navigationController pushViewController:viewCon animated:YES];
}
else if ([urlStr containsString:@"timeLine"])
{
// 界面三
ThirdViewController *viewCon = [[ThirdViewController alloc] init];
viewCon.urlPath =urlStr;
[rootVc.navigationController pushViewController:viewCon animated:YES];
}
return YES;
}
@end
应用B在界面二(SecondViewController)
与界面三(ThirdViewController)
中代码相同,所以下面只列出界面二(SecondViewController)
的代码,如下:
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"界面二";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
}
- (void)clickLeftButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath substringFromIndex:location + 1];
// 通过scheme返回项目A
NSString *news = [NSString stringWithFormat:@"%@://cancel", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl])
{
[[UIApplication sharedApplication] openURL:newsUrl];
}
}
- (void)clickRightButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath substringFromIndex:location + 1];
// 通过scheme返回项目A
NSString *news = [NSString stringWithFormat:@"%@://confirm", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl])
{
[[UIApplication sharedApplication] openURL:newsUrl];
}
}
@end
4、总结:
本篇文章,只是技术实现上的简单演示。
在实际的应用中,应用间的跳转要远比这个复杂很多,也严谨很多。
但是其实现的原理,也只是在这个原理基础上更加复杂的扩展而已,所以,希望这里对大家有所帮助。
5、参考文献:
1、http://www.jianshu.com/p/732c5e1720d0
2、http://lidaoliang.com/14429923363009.html
3、http://www.jianshu.com/p/545014e51ad5