canOpenURL:
方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes
,键值内容是对应应用程序的URL Schemes
。具体设置如下图:UIApplication
的canOpenURL
方法,协议头后面可以拼接参数传递过去,如下所示:NSURL * appurl = [NSURL URLWithString:@"Bapp://"];
//判断是否安装了对应的应用,安装了就打开
if ([[UIApplication sharedApplication] canOpenURL:appurl]) {
[[UIApplication sharedApplication]openURL:appurl];
}else{
UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
[alertV show];
}
5.当A_app调用了canOpenURL
方法,B_app会响应- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary
代理方法,在该代理方法中可以获取传过来的URL,通过拦截url传过来的参数可以处理跳到对应的模块。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)jumpToHome:(id)sender {
//注意设置URL Schemes 事里面不要有“—”,否则可能会跳不过去
NSURL * appurl = [NSURL URLWithString:@"Bapp://"];
//判断是否安装了对应的应用,安装了就打开
if ([[UIApplication sharedApplication] canOpenURL:appurl]) {
[[UIApplication sharedApplication]openURL:appurl];
}else{
UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
[alertV show];
}
}
- (IBAction)jumpToBlue:(id)sender {
NSURL * appurl = [NSURL URLWithString:@"Bapp://BlueViewController"];
if ([[UIApplication sharedApplication] canOpenURL:appurl]) {
//iOS10以后过期
[[UIApplication sharedApplication]openURL:appurl];
}else{
UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
[alertV show];
}
}
- (IBAction)jumpToPurple:(id)sender {
NSURL * appurl = [NSURL URLWithString:@"Bapp://SubViewController"];
if ([[UIApplication sharedApplication]canOpenURL:appurl]) {
//iOS10以后有效
[[UIApplication sharedApplication]openURL:appurl options:nil completionHandler:nil];
}
}
@end
应用程序B_app中文件结构和AppDelegate主要实现方法:
主要实现的代理方法:
#import "AppDelegate.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];
UITabBarController * tabbarVC = [[UITabBarController alloc]init];
NSArray * rootVCNameArray = [NSArray arrayWithObjects:@"RedViewController",@"GreenViewController",@"BlueViewController", nil];
for (NSString * vcName in rootVCNameArray) {
UIViewController * vc = [[NSClassFromString(vcName) alloc]init];
UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:vc];
navVC.navigationBar.tintColor = [UIColor brownColor];
navVC.title = vcName;
[tabbarVC addChildViewController:navVC];
}
self.window.rootViewController = tabbarVC;
[self.window makeKeyAndVisible];
return YES;
}
//当其他应用跳转到当前应用就会调用该代理方法,iOS9及以后有效
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
//获取跟控制器
UITabBarController * tabbarVC = (UITabBarController *)self.window.rootViewController;
NSLog(@"scheme : %@",url.scheme);
NSLog(@"absoluteString : %@",url.absoluteString);
if ([url.absoluteString containsString:@"BlueViewController"]) {
tabbarVC.selectedIndex = 2;
}else if ([url.absoluteString containsString:@"SubViewController"]){
tabbarVC.selectedIndex = 0;
UIViewController * vc = [[NSClassFromString(@"SubViewController") alloc]init];
[tabbarVC.childViewControllers.firstObject pushViewController:vc animated:YES];
}
return YES;
}
//-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
// return YES;
//}
//-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//
// return YES;
//}
@end
最终效果图:
如果想跳回A_app,可以添加A的URL Schemes,在B_app中添加白名单,调用openURL
方法即可。
@{"APP":@"打电话",@"Schemes":@"tel"},
@{@"APP":@"发短信",@"Schemes":@"sms"},
@{@"APP":@"打开日历",@"Schemes":@"calshow"},
@{@"APP":@"提醒",@"Schemes":@"x-apple-reminder"},
@{@"APP":@"邮件",@"Schemes":@"message"},
@{@"APP":@"iTunes Store",@"Schemes":@"itms"},
@{@"APP":@"App Store",@"Schemes":@"itms-apps"},
@{@"APP":@"iBooks",@"Schemes":@"ibooks"},
@{@"APP":@"Facetime",@"Schemes":@"facetime"}
LSApplicationQueriesSchemes
wechat
weixin
sinaweibohd
sinaweibo
sinaweibosso
weibosdk
weibosdk2.5
mqqapi
mqq
mqqOpensdkSSoLogin
mqqconnect
mqqopensdkdataline
mqqopensdkgrouptribeshare
mqqopensdkfriend
mqqopensdkapi
mqqopensdkapiV2
mqqopensdkapiV3
mqzoneopensdk
wtloginmqq
wtloginmqq2
mqqwpa
mqzone
mqzonev2
mqzoneshare
wtloginqzone
mqzonewx
mqzoneopensdkapiV2
mqzoneopensdkapi19
mqzoneopensdkapi
mqzoneopensdk
alipay
alipayshare
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic ,copy) NSArray * dataSource;
@end
@implementation ViewController
-(NSArray *)dataSource{
if (!_dataSource) {
_dataSource = @[
@{@"APP":@"打电话",@"Schemes":@"tel"},
@{@"APP":@"发短信",@"Schemes":@"sms"},
@{@"APP":@"打开日历",@"Schemes":@"calshow"},
@{@"APP":@"提醒",@"Schemes":@"x-apple-reminder"},
@{@"APP":@"邮件",@"Schemes":@"message"},
@{@"APP":@"iTunes Store",@"Schemes":@"itms"},
@{@"APP":@"App Store",@"Schemes":@"itms-apps"},
@{@"APP":@"iBooks",@"Schemes":@"ibooks"},
@{@"APP":@"Facetime",@"Schemes":@"facetime"},
@{@"APP":@"微信",@"Schemes":@"wechat"},
@{@"APP":@"微博",@"Schemes":@"sinaweibo"},
@{@"APP":@"QQ",@"Schemes":@"mqq"},
@{@"APP":@"支付宝",@"Schemes":@"alipay"},
];
}
return _dataSource;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCell"];
NSDictionary * dic = self.dataSource[indexPath.row];
cell.textLabel.text = [dic valueForKey:@"APP"];
cell.detailTextLabel.text = [dic valueForKey:@"Schemes"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary * dic = self.dataSource[indexPath.row];
NSString * schemes = [dic valueForKey:@"Schemes"];
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://",schemes]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url options:nil completionHandler:nil];
}
}
@end
效果图如下: