歡迎你們關注個人公衆號,我會按期分享一些我在項目中遇到問題的解決辦法和一些iOS實用的技巧,現階段主要是整理出一些基礎的知識記錄下來
javascript
文章也會同步更新到個人博客:
ppsheep.comjava
咱們先來看一下效果圖git
在開發之中咱們常常遇到須要從咱們當前程序跳入到另一個程序中,下面咱們來講明一下這實現的過程github
咱們想要打開iOS中的一個應用,只須要拿到這個應用的協議頭,咱們就能夠實現從一個APP跳到另一個APPxcode
假設咱們如今有兩個APP,APP-1和APP-2,須要從APP-1跳到APP-2
那麼咱們就須要給APP-2綁定一個URL Schemes(自定義的應用協議頭),在APP-1中咱們就能夠實現用這個協議頭跳到APP-2微信
建立兩個項目APP-1和APP-2app
在APP-2中,咱們來綁定URL Schemeside
在APP2的target中綁定協議頭爲 APP2url
而後咱們在APP-1中添加一個按鈕 來實現,點擊跳轉spa
注意,這兩個APP得在手機上都安裝上,才能跳轉
[btn1 addTarget:self action:@selector(jumpToApp2) forControlEvents:UIControlEventTouchUpInside];複製代碼
跳轉方法
- (void)jumpToApp2{
//獲取APP2的URL Scheme
NSURL *appURL = [NSURL URLWithString:@"APP2://"];
//判斷手機中是否安裝了APP2
if ([[UIApplication sharedApplication] canOpenURL:appURL]) {
//打開APP2
[[UIApplication sharedApplication] openURL:appURL];
}else{
NSLog(@"沒有安裝APP2");
}
// ---------------若是是iOS9以前,上面代碼就能夠實現跳轉了---------------------
// ---------------iOS9以後須要在應用程序中加上白名單,即在APP-1中加上APP-2的白名單---------
// ----------------在APP-1的Info中 添加鍵值對LSApplicationQueriesSchemes---------------------------------
}複製代碼
在iOS9以後,跳轉 咱們須要在APP中設置須要跳轉的URL Schemes才能實現跳轉 即在Info中添加LSApplicationQueriesSchemes鍵值對
而後咱們就能夠實現跳轉了
看一下效果
這個實現起來也很簡單的,首先在APP2中新建兩個ViewController
而後在APP-1中添加兩個按鈕分別是跳轉到不一樣的頁面
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 200, 30)];
[btn1 setTitle:@"跳轉到第一個界面" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(jumpToApp2_1) forControlEvents:UIControlEventTouchUpInside];
btn1.backgroundColor = [UIColor redColor];
[self.view addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 200, 30)];
[btn2 setTitle:@"跳轉到第二個界面" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(jumpToApp2_2) forControlEvents:UIControlEventTouchUpInside];
btn2.backgroundColor = [UIColor redColor];複製代碼
跳轉方法
- (void)jumpToApp2_1{
//獲取APP2的URL Scheme
NSURL *appURL = [NSURL URLWithString:@"APP2://VC1"];
//判斷手機中是否安裝了APP2
if ([[UIApplication sharedApplication] canOpenURL:appURL]) {
//打開APP2的ViewController1
[[UIApplication sharedApplication] openURL:appURL];
}else{
NSLog(@"沒有安裝APP2");
}
// ---------------若是是iOS9以前,上面代碼就能夠實現跳轉了---------------------
// ---------------iOS9以後須要在應用程序中加上白名單,即在APP-1中加上APP-2的白名單---------
// ----------------在APP-1的Info中 添加鍵值對LSApplicationQueriesSchemes---------------------------------
}
- (void)jumpToApp2_2{
//獲取APP2的URL Scheme
NSURL *appURL = [NSURL URLWithString:@"APP2://VC2"];
//判斷手機中是否安裝了APP2
if ([[UIApplication sharedApplication] canOpenURL:appURL]) {
//打開APP2ViewController2
[[UIApplication sharedApplication] openURL:appURL];
}else{
NSLog(@"沒有安裝APP2");
}
// ---------------若是是iOS9以前,上面代碼就能夠實現跳轉了---------------------
// ---------------iOS9以後須要在應用程序中加上白名單,即在APP-1中加上APP-2的白名單---------
// ----------------在APP-1的Info中 添加鍵值對LSApplicationQueriesSchemes---------------------------------
}複製代碼
經過對跳轉的協議頭添加不一樣的描述,來實現不一樣的跳轉
而後咱們在APP-2的delegate中 實現方法
// 由於如今xcode8 最低支持8.0 因此 我仍是用這個方法吧
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//獲取根控制器
UINavigationController *nvc = (UINavigationController *)self.window.rootViewController;
// ViewController *vc = nvc.childViewControllers.firstObject;
//每次跳轉都必需要在根控制器 這一點比較重要
[nvc popToRootViewControllerAnimated:YES];
//根據url判斷須要展現的VC
if ([url.absoluteString containsString:@"VC1"]) {
ViewController1 *vc1 = [[ViewController1 alloc] init];
// [vc presentViewController:vc1 animated:YES completion:nil];
[nvc pushViewController:vc1 animated:YES];
}else if([url.absoluteString containsString:@"VC2"]){
ViewController2 *vc2 = [[ViewController2 alloc] init];
[nvc pushViewController:vc2 animated:YES];
}
return YES;
}複製代碼
實現跳轉監聽,這樣就能跳轉到不一樣的頁面了
源代碼放在了