网页唤醒app:
可以通过URL Schemes唤醒某个应用。具体schemes是什么,怎么理解,可自行百度,我觉得可以将它认为是某个应用的身份标识。当然,这个标识可能不是唯一的,因为人们可以自定义它,一个人定义一个应用的schemes为myApp,另外一个人也可以定义另一个应用的schemes为myApp。
新建一个个人应用:
然后TARGETS->info->URL Types->点击"+"号,这里定义URL Schemes为openApp。
至此,若仅仅是网页唤醒应用,工作已经完成。
若在工程的info.plist文件中查看,则是:
F2DF950E-5B8F-4B96-BC08-84856D1E8CA3.png即:URL types->item0->URL Schemes->item0
PS: URL Schemes可定义多个。
AppDelegate.m:
//
// AppDelegate.m
// OpenApp
//
// Created by mac on 17/4/28.
// Copyright © 2017年 cai. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
//iOS8及以下这个
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return YES;
}
//iOS9及以上这个
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
NSLog(@"--url: %@", url);
return YES;
}
@end
Output:
2017-04-28 14:39:31.684442+0800 OpenApp[14303:3806016] --url: openapp://
可以在URL Schemes指令: openApp后添加参数,如: openApp://www.baidu.com
则被打开的应用的输出为:
2017-04-28 14:44:21.115314+0800 OpenApp[14303:3806016] --url: openapp://www.baidu.com
然后可取到参数,做相应操作。
效果:
IMG_3209.jpgIMG_3210.jpg
IMG_3211.jpg
一个app唤醒另一个app:
新建两个个人应用分别为: OpenAppA和OpenAppB,前者唤醒后者为例。另外唤醒两个已经上线的应用: 滴滴企业版和滴滴个人版。
(我的手机未安装滴滴企业版、但安装了滴滴个人版和新建的OpenAppB应用)
首先看OpenAppA应用的配置:
在其info.plist中配置:
PS: 滴滴企业版的URL Schemes为didies、滴滴个人版的URL Schemes为onetravel。我这里定义应用OpenAppB的URL Schemes为openAppB,这个可以自定义。
ViewController.m:
//
// ViewController.m
// OpenAppA
//
// Created by mac on 17/4/28.
// Copyright © 2017年 cai. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"OpenAppA";
self.view.backgroundColor = [UIColor orangeColor];
[self testPushDidi];
}
#pragma mark -create UI
- (void)testPushDidi
{
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"滴滴企业版" forState:UIControlStateNormal];
btn1.frame = CGRectMake(10, 100, 100, 50);
btn1.backgroundColor = [UIColor purpleColor];
[btn1 addTarget:self action:@selector(btn1Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setTitle:@"滴滴个人版" forState:UIControlStateNormal];
btn2.frame = CGRectMake(10, 200, 100, 50);
btn2.backgroundColor = [UIColor purpleColor];
[btn2 addTarget:self action:@selector(btn2Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn3 setTitle:@"openAppB" forState:UIControlStateNormal];
btn3.frame = CGRectMake(10, 300, 100, 50);
btn3.backgroundColor = [UIColor purpleColor];
[btn3 addTarget:self action:@selector(btn3Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
}
- (void)btn1Action
{
NSString *str = @"didies://";
NSURL *url = [NSURL URLWithString:str];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"can not--滴滴企业版");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/di-di-qi-ye-ban-mei-hao-shang/id1061542715?mt=8"]];
}
}
- (void)btn2Action
{
NSString *str = @"onetravel://";
NSURL *url = [NSURL URLWithString:str];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"can not--滴滴个人版");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/di-di-da-che-da-che-shen-qi/id554499054?mt=8"]];
}
}
- (void)btn3Action
{
NSString *str = @"openAppB://";
NSURL *url = [NSURL URLWithString:str];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"can not");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
图:
IMG_3214.jpgOpenAPPB应用:
只需要配置自己的URL Schemes为openAppB即可。
屏幕快照 2017-04-28 16.03.08.png效果:
- 点击滴滴企业版按钮
因为未安装滴滴企业版,则进入了else跳转到App Store的滴滴企业版下载页面;
Output:
2017-04-28 16:04:36.825447+0800 OpenAppA[14375:3822064] -canOpenURL: failed for URL: "didies://" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
2017-04-28 16:04:36.825635+0800 OpenAppA[14375:3822064] can not--滴滴企业版
- 点击滴滴个人版按钮
本地安装了滴滴个人版,则打开应用。
- 点击openAppB按钮
与打开滴滴个人版效果一致
网友评论