iOS應用跳轉

聲明:
本文章是參考:船長_的文章:http://www.lxweimin.com/p/732c5e1720d0 所寫,請大家方便時移步船長_ 的博客為他點贊。
謝謝!

iOS 應用之間的跳轉,是通過設置"URL Scheme"來實現的。
簡單來說:假設現在有兩個應用A與B,我們分別設置了A和B的"URL Scheme",作為這兩個App的標識,并通過在A和B的程序中一些設置,來達到應用跳轉的目的。

具體的操作流程如下:
1、首先設置A與B的"URL Scheme":

1、A_URL.png

2、B_URL.png

2、將A和B的"URL Scheme"添加到對方的白名單中:
蘋果公司在 iOS9之后 引入白名單概念。
在iOS9中,如果使用 canOpenURL: 方法,該方法所涉及到的 URL scheme 必須在"Info.plist"中將它們列為白名單,否則不能使用。(白名單上限是50個)。
3、A_B_URLScheme.png

4、B_A_URLScheme.png

PS:在 iOS9 之前,你可以使用
[[UIApplication sharedApplication] canOpenURL:url]
監測用戶手機里到底裝沒裝某些應用,來給用戶提示。
但是也有一些別有用心的App ,這些App有一張常用 App 的"URL scheme"列表,他們會多次調用
[[UIApplication sharedApplication] canOpenURL:url]
遍歷該表,來監測用戶手機都裝了什么 App 。
比如:"你"裝了叫“大姨媽”的App,他就可以知道"你"是女性,他就可以只推給"你"女性用品的廣告。
這是屬于侵犯用戶隱私的行為,可能就是導致蘋果推出白名單的原因。
3、界面、演示及代碼:
3.1、界面:
首先看一下A應用的界面:
5、A_UI.png

5.1、A_cancel.png

5.2、A_comfirm.png

B應用的界面為:
6.1、B_UI_1.png

6.2、B_UI_2.png

6.3、B_UI_3.png

3.2、演示圖如下:
demo.gif

3.3、代碼:
A:
在A應用的Appdelegate.m中,代碼如下:

#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.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];
    UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = rootNav;
    [self.window makeKeyAndVisible];
    return YES;
}

// 過期
- (BOOL)application:(UIApplication *)application handleOpenURL: (NSURL *)url
{
    NSLog(@"application.1 == %@",application);
    NSLog(@"url.1         == %@",url);
    return YES;
}

// 過期
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"application.2       == %@",application);
    NSLog(@"url.2               == %@",url);
    NSLog(@"sourceApplication.2 == %@",sourceApplication);
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (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;

    if ([urlStr containsString:@"confirm"])
    {
        // 確定界面
        SecondViewController *viewCon = [[SecondViewController alloc] init];
        [rootVc.navigationController pushViewController:viewCon animated:YES];

    }
    else if ([urlStr containsString:@"cancel"])
    {
        // 取消界面
        ThirdViewController *viewCon = [[ThirdViewController alloc] init];
        [rootVc.navigationController pushViewController:viewCon animated:YES];
    }
    return YES;
}
@end

在A應用的ViewController.m中,代碼如下:

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"項目A";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"跳轉B應用" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    button.frame = CGRectMake(0, 0, 100, 20);
    button.center = CGPointMake(self.view.frame.size.width/2, 100);
    
    [button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonTwo setTitle:@"跳轉B界面二" forState:UIControlStateNormal];
    [self.view addSubview:buttonTwo];
    
    buttonTwo.frame = CGRectMake(0, 0, 100, 20);
    buttonTwo.center = CGPointMake(self.view.frame.size.width/2, 160);
    
    [buttonTwo addTarget:self action:@selector(jumpViewTwo:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonThree = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonThree setTitle:@"跳轉B界面三" forState:UIControlStateNormal];
    [self.view addSubview:buttonThree];
    
    buttonThree.frame = CGRectMake(0, 0, 100, 20);
    buttonThree.center = CGPointMake(self.view.frame.size.width/2, 220);
    
    [buttonThree addTarget:self action:@selector(jumpViewThree:) forControlEvents:UIControlEventTouchUpInside];
    
}
// 直接跳轉B應用
- (void)jump:(UIButton *)button
{
    NSURL *url = [NSURL URLWithString:@"ObjectB://"];
    
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
    }
}
// 跳轉B應用界面二
- (void)jumpViewTwo:(UIButton *)button
{
    // 注意:這里將應用A的URL當做參數傳給應用B
    NSURL *url = [NSURL URLWithString:@"ObjectB://session?ObjectA"];
    
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
    }
}
// 跳轉B應用界面三
- (void)jumpViewThree:(UIButton *)button
{
    // 注意:這里將應用A的URL當做參數傳給應用B
    NSURL *url = [NSURL URLWithString:@"ObjectB://timeLine?ObjectA"];
    
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
    }
}
@end

B:
應用B在Appdelagate.m中,代碼如下:

#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.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];
    
    UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] init]];

    self.window.rootViewController = rootNav;
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL: (NSURL *)url
{
    NSLog(@"application.1 == %@",application);
    NSLog(@"url.1         == %@",url);
    
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"application.2       == %@",application);
    NSLog(@"url.2               == %@",url);
    NSLog(@"sourceApplication.2 == %@",sourceApplication);
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (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;
    
    if ([urlStr containsString:@"session"])
    {
        // 界面二
        SecondViewController *viewCon = [[SecondViewController alloc] init];
        viewCon.urlPath =urlStr;
        [rootVc.navigationController pushViewController:viewCon animated:YES];
    }
    else if ([urlStr containsString:@"timeLine"])
    {
        // 界面三
        ThirdViewController *viewCon = [[ThirdViewController alloc] init];
        viewCon.urlPath =urlStr;
        [rootVc.navigationController pushViewController:viewCon animated:YES];
    }

    return YES;
}
@end

應用B在界面二(SecondViewController)界面三(ThirdViewController)中代碼相同,所以下面只列出界面二(SecondViewController)的代碼,如下:

#import "SecondViewController.h"
@interface SecondViewController ()
@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"界面二";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
}

- (void)clickLeftButton
{
    // 截取字符串,拿到scheme
    NSInteger location = [self.urlPath rangeOfString:@"?"].location;
    NSString *scheme = [self.urlPath substringFromIndex:location + 1];
    
    // 通過scheme返回項目A
    NSString *news = [NSString stringWithFormat:@"%@://cancel", scheme];
    NSURL *newsUrl = [NSURL URLWithString:news];
    
    if ([[UIApplication sharedApplication] canOpenURL:newsUrl])
    {
        [[UIApplication sharedApplication] openURL:newsUrl];
    }
}

- (void)clickRightButton
{
    // 截取字符串,拿到scheme
    NSInteger location = [self.urlPath rangeOfString:@"?"].location;
    NSString *scheme = [self.urlPath substringFromIndex:location + 1];
    
    // 通過scheme返回項目A
    NSString *news = [NSString stringWithFormat:@"%@://confirm", scheme];
    NSURL *newsUrl = [NSURL URLWithString:news];
    
    if ([[UIApplication sharedApplication] canOpenURL:newsUrl])
    {
        [[UIApplication sharedApplication] openURL:newsUrl];
    }
}
@end

4、總結:
本篇文章,只是技術實現上的簡單演示。
在實際的應用中,應用間的跳轉要遠比這個復雜很多,也嚴謹很多。
但是其實現的原理,也只是在這個原理基礎上更加復雜的擴展而已,所以,希望這里對大家有所幫助。
5、參考文獻:
1、http://www.lxweimin.com/p/732c5e1720d0
2、http://lidaoliang.com/14429923363009.html
3、http://www.lxweimin.com/p/545014e51ad5

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內容