- 场景需求:一个应用A(以news应用为示例)跳转到另外一个应用B(以weChat为示例),常见需求如下
- 1.应用推荐
- 2.支付宝支付
- 3.第三方登录
- 4.微信分享
- 注意:iOS9中打开一个应用程序的URL必须配置info.plist文件
- 添加LSApplicationQueriesSchemes的key
- 添加对应url的scheme
一: 打开系统的应用程序
打开打电话应用程序
URL:tel://电话号码
打开发短信应用程序
URL:sms://电话号码
打开系统的设置界面,必须先在info.plist中配置URL Schemes
在URL Types中添加prefs
打开Wifi设置
URL:prefs:root=WIFI
打开定位服务
URL:prefs:root=LOCATION_SERVICES
打开蓝牙服务
URL:prefs:root=Bluetooth
打开FaceTime
URL:prefs:root=FACETIME
打开音乐
URL:prefs:root=MUSIC
打开墙纸设置
URL:prefs:root=Wallpaper
配置如图下图
- 分析:核心代码:
[[UIApplication sharedApplication] openURL:url]
一:应用A跳转应用B
1.界面搭建如图
2.A应用打开B应用的方法
- (IBAction)jumpToweChat:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
- (IBAction)jumpToFriends:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://session?news"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
- (IBAction)jumpToTimeLIne:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://timeLine?news"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
2.设置应用A和应用B的URL Types中的URL Schemes
3.在应用B中监听跳转,进行判断,执行不同的跳转
- 在AppDelegate中实现下面的方法监听
// 已过期
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
// 已过期
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
/**
* 通过别的应用打开我们的应用时会来到这里
* @param url 通过什么URL打开的
* @param sourceApplication 打开我们应用的sourceApplication
*/
- (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;
// 传值
rootVc.urlPath = urlStr;
if ([urlStr containsString:@"session"]) { // 好友界面
[rootVc performSegueWithIdentifier:@"session" sender:nil];
}else if ([urlStr containsString:@"timeLine"]) {// 朋友圈界面
[rootVc performSegueWithIdentifier:@"timeLine" sender:nil];
}
return YES;
}
4.从应用B跳转到应用A
// 这里用storyboard做的,监听程序的跳转
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"session"]) {
SessionViewController *vc = segue.destinationViewController;
vc.urlPath = self.urlPath;
}else if ([segue.identifier isEqualToString:@"timeLine"]) {
TimeLineViewController *vc = segue.destinationViewController;
vc.urlPath1 = self.urlPath;
}
}
5.微信朋友圈界面设置
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"微信朋友圈";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}
- (void)clickLeftButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath1 rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath1 substringFromIndex:location + 1];
// 通过scheme返回新闻
NSString *news = [NSString stringWithFormat:@"%@://", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
[[UIApplication sharedApplication] openURL:newsUrl];
}
}
6.微信好友界面设置
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"微信好友界面";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}
- (void)clickLeftButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath substringFromIndex:location + 1];
// 通过scheme返回新闻
NSString *news = [NSString stringWithFormat:@"%@://", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
[[UIApplication sharedApplication] openURL:newsUrl];
}
}