系统的URL schemes的优先级高于用户app定义的,所以重名时,系统会按自己方式跳转处理。
NSURL *url = [NSURL URLWithString:@"myapp:"]; [[UIApplication sharedApplication] openURL:url];
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if ([[url scheme] isEqualToString:@"myapp"]) { //处理链接 return YES; } return NO; }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //被其他应用调用 NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]; if(url) {//做出相应的判断 if ([[url scheme] isEqualToString:@"myapp"]) { //处理链接 } } self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto://[email protected]"]];
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"tel://8008808888"]];
[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://www.hzlzh.com"]];
[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"remote://fff"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/cn/app/bai-du-wen-kuhd/id483064532?mt=8"]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itunes.apple.com/cn/app/bai-du-wen-kuhd/id483064532?mt=8"]]
定义两个app,模拟应用间跳转,一个app为新浪,一个app为网易
// // AppDelegate.m // 01-新浪 // // Created by apple on 14/11/2. // Copyright (c) 2014年 heima. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate -(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions { return YES; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } /** * 当被其他应用程序通过URL打开时就会调用 * * @param application 当前的应用程序 * @param url 打开当前程序的URL * * @return 是否成功处理 */ - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { // 1.获取首页控制器 UINavigationController *nav = (UINavigationController *)self.window.rootViewController; UIViewController *vc = nav.topViewController; NSLog(@"%@", url); // 判断是通过哪一个URL打开的, 做出相应的处理(跳转到相应的控制器) NSString *urlStr = url.absoluteString; if ([urlStr hasPrefix:@"sina://login"]) { // 截取打开我们程序的应用的scheme NSRange range = [urlStr rangeOfString:@"sina://login?myScheme="]; NSString *scheme = [urlStr substringFromIndex:range.length]; NSLog(@"跳转到授权界面 %@", scheme); // if ([vc isKindOfClass:[ViewController class]] == YES) { [vc performSegueWithIdentifier:@"home2accounts" sender:scheme]; // } }else if ([urlStr hasPrefix:@"sina://view?id="]) { NSLog(@"跳转到详情界面"); [vc performSegueWithIdentifier:@"home2detail" sender:nil]; } return YES; } /** * 当被其他应用程序通过URL打开时就会调用(新方法) * * @param application 当前的应用程序 * @param url 打开当前程序的URL * @param sourceApplication 打开当前程序的Bundle identifier * @param annotation * * @return 是否成功处理 */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { #warning 注意: 如果实现了新方法, 旧方法旧失效了 /* sourceApplication用途: 当我们做一些广告, 积分墙等推广的时候, 可以利用这个唯一表一记录当前程序是被哪一个程序推广打开的 */ NSLog(@"%@ %@", url, sourceApplication); // 1.获取首页控制器 UINavigationController *nav = (UINavigationController *)self.window.rootViewController; UIViewController *vc = nav.topViewController; NSLog(@"%@", url); // 判断是通过哪一个URL打开的, 做出相应的处理(跳转到相应的控制器) NSString *urlStr = url.absoluteString; if ([urlStr hasPrefix:@"sina://login"]) { // 截取打开我们程序的应用的scheme NSRange range = [urlStr rangeOfString:@"sina://login?myScheme="]; NSString *scheme = [urlStr substringFromIndex:range.length]; NSLog(@"跳转到授权界面 %@", scheme); // if ([vc isKindOfClass:[ViewController class]] == YES) { [vc performSegueWithIdentifier:@"home2accounts" sender:scheme]; // } }else if ([urlStr hasPrefix:@"sina://view?id="]) { NSLog(@"跳转到详情界面"); [vc performSegueWithIdentifier:@"home2detail" sender:nil]; } return YES; } @end
// // ViewController.m // 01-新浪 // // Created by apple on 14/11/2. // Copyright (c) 2014年 heima. All rights reserved. // #import "ViewController.h" #import "TableViewController.h" @interface ViewController () - (IBAction)openWangYi; @end @implementation ViewController /** * 打开网易新闻 */ - (IBAction)openWangYi { // 1.获取application对象 UIApplication *app = [UIApplication sharedApplication]; // 2.创建需要打开的应用程序的URL // 在应用程序跳转中, 只要有协议头即可, 路径可有可无 NSURL *url = [NSURL URLWithString:@"wangyi://"]; // 3.利用application打开URL if ([app canOpenURL:url]) { // 3.1判断是否可以打开 [app openURL:url]; }else { // 3.2打开App STORE下载 NSLog(@"根据App id打开App STORE"); } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"首页 %@", sender); UIViewController *vc = segue.destinationViewController; if ([vc isKindOfClass:[TableViewController class]]) { // 如果跳转的目标控制器是授权,才需要设置Scheme TableViewController *tbVc = vc; tbVc.callScheme = sender; } } @end
// // ViewController.m // 02-网易 // // Created by apple on 14/11/2. // Copyright (c) 2014年 heima. All rights reserved. // #import "ViewController.h" @interface ViewController () - (IBAction)openSina; - (IBAction)openDetail; @end @implementation ViewController /** * 打开新浪授权界面 */ - (IBAction)openSina { // 每个程序都可以拥有一个自己唯一的URL // URL组成: 协议头://主机/路径 // http:// // file:// // ftp:// // ... // 1.获取application对象 UIApplication *app = [UIApplication sharedApplication]; // 2.创建需要打开的应用程序的URL // 在应用程序跳转中, 只要有协议头即可, 路径可有可无 NSURL *url = [NSURL URLWithString:@"sina://login?myScheme=wangyi"]; // 3.利用application打开URL if ([app canOpenURL:url]) { // 3.1判断是否可以打开 [app openURL:url]; }else { // 3.2打开App STORE下载 NSLog(@"根据App id打开App STORE"); } } /** * 打开新浪详情界面 */ - (IBAction)openDetail { // 1.获取application对象 UIApplication *app = [UIApplication sharedApplication]; // 2.创建需要打开的应用程序的URL // 在应用程序跳转中, 只要有协议头即可, 路径可有可无 NSURL *url = [NSURL URLWithString:@"sina://view?id=123456"]; // 3.利用application打开URL if ([app canOpenURL:url]) { // 3.1判断是否可以打开 [app openURL:url]; }else { // 3.2打开App STORE下载 NSLog(@"根据App id打开App STORE"); } } @end