什么是 URL schemes ?
iOS began supporting URL schemes a couple years ago; the feature enables webpage links or apps to launch other apps on the phone.
iOS实际上数年前便已经支持这种识别功能,这种功能可以通过内置网页链接的方式,在网页中直接激活手机上的其它应用程序。
URL Schemes 在工程中的位置
例子1:testApp1跳到testApp2,并且进行了传值(涉及到App之间的跳转、通知中心、转码)
testApp1的ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 40)];
[button setTitle:@"跳到testApp2" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick
{
/*
testApp2 的info里面的URL Schemes里面配置 com.uen.testApp2
urlString为com.uen.testApp2://
*/
//NSString *urlString=@"com.uen.testApp2://";
//NSString *urlString=@"com.uen.testApp2://appid=12345&name=微信"; //需要转码
NSString *urlString=@"com.uen.testApp2://appid=12345&name=weixin"; //带参数
//转码,中文和表情符号会被转码,数字、字符串不变
//NSString *string1=[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@""]];
//解码方法一
//NSString *string2=[string1 stringByRemovingPercentEncoding];
//解码方法二
//NSString *string3=[string1 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
testApp2的AppDelegate.m文件
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
//这两个方法都要写
//9.0之后从其它APP跳转到此APP的时候,会调用此方法
//url 表示第一个APP调转到此APP时写的url,我们可以利用url来做APP之间的传值
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
//更具url的不同做不同的事情
/*例子:
1、从不在APP的一个网页跳转到APP某一页,如果没有安装APP就跳到APP store。
1)、跳到APP或者APP store ,这件事情是web前端做的,我们需要提供info中urlschemes
2)、更具url中的参数做不同的事情
例如:点击不在APP上的一个网址上,可以跳到爱限免的详情页。
1]、web端需要传一个appId在url中
2]、在此方法中拿到appId之后,做跳转操作(常用通知中心)
2、从一个APP跳到另外一个APP
1)、两个APP都是自己公司的,做法如1
2)、跳到第三方公司的APP,第三方公司的APP也要跳会你的APP,这些需要严格按照第三方公司的文档来
例子:微信分享、朋友圈分享、qq等分享,支付、第三方登录......
*/
//url是不支持中文的,如果需要传中文,需要将中文转码,用的时候再解码
//url.absoluteString 绝对路径
//url.relativeString 相对路径 如何url的baseurl为nil,绝对路径和相对路径的值一样
[[NSNotificationCenter defaultCenter] postNotificationName:@"qwer" object:nil userInfo:@{@"url":url.relativeString}];
return YES;
}
//4.2~9.0调用此方法
//url 表示第一个APP调转到此APP时写的url,我们可以利用url来做APP之间的传值
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
testApp2的ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
{
UILabel *_label;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor=[UIColor redColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabel:) name:@"qwer" object:nil];
_label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 40)];
_label.text=@"我是第二个APP,但我不二";
_label.textColor=[UIColor yellowColor];
_label.font=[UIFont systemFontOfSize:20];
_label.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:_label];
}
- (void)changeLabel:(NSNotification *)not
{
_label.text=not.userInfo[@"url"];
//_label.text = [not userInfo][@"url"];
}
- (void)dealloc {//MRC 需要写[super dealloc];
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"qwer" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
当然testApp2的URLSchemes配置:
总结App之间跳转的思路
APP跳转的思路
例子:X 跳到 Y
1、Y首先需要在info->urltypes里面点"+",在出现的视图中的"URL schemes"中写上标示(如:"YURLSchemes")
2、Y在AppDelegate里面实现代理方法
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{}
参数URL是X跳到Y时的URL
3、Y根据URL和相应的事情,这块地方可能用到的知识点有通知中心、模态化呈现视图([self.window.rootViewContrller present...])
4、X使用[[UIApplication shareApplication] openURL:url] (@"Y的url schemes://需要传的值",如果需要传的值是中文,需要转码)