一個程序若要跳到另一個程序。需要在目標程序的plist文件里面修改:
打開info.plist,添加一項URL types
展開URL types,再展開Item0,將Item0下的URL identifier修改為URL Scheme
展開URL Scheme,將Item0的內容修改為 SecondApp(此為跳轉的key)
話不多說,下面開始講解步驟:
首先創建兩個工程,第一個 FirstAPP , 第二個 SecondAPP
第一個 First APP 的 info.plist 需要設置 key(url) 與 白名單
接下來我們再對第二個 SecondAPP 工程來做相應的處理
將這兩個工程設置好了之后,接下來上代碼
第一個 FirstApp工程
//
// ViewController.m
// FirstAPP
//
// Created by luorende on 16/8/25.
// Copyright © 2016年 luorende. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 200, 50);
button.backgroundColor = [UIColor darkGrayColor];
[button setTitle:@"跳轉到SecondApp" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:20];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//跳轉到SecondApp
-(void)clickButton:(UIButton *)button{
NSLog(@"執行了點擊事件");
//之前配置的白名單,就是需要跳轉對方App的key,即對方設置的url
NSString * UrlStr = @"SecondApp://xxxxx";
NSURL * url = [NSURL URLWithString:UrlStr];
// 在這里可以先做個判斷
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"應用程序未安裝");
}
}
//跳轉到AppStore
-(void)abc{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@""]];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@""]];
}
第二個工程 SecondAPP 里的代碼
//
// ViewController.m
// SecondAPP
//
// Created by luorende on 16/8/26.
// Copyright © 2016年 luorende. 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.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 200, 100);
button.backgroundColor = [UIColor darkGrayColor];
[button setTitle:@"SecondApp,跳轉到另一個APP" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:20];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)clickButton:(UIButton *)button{
NSLog(@"執行了點擊事件");
NSString * UrlStr = @"FirstAPP://xxxxx";
NSURL * url = [NSURL URLWithString:UrlStr];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else{
NSLog(@"應用程序未安裝");
// 程序未成功跳轉,我們還可以做一個提示
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"應用程序未安裝"message:@"確定下載<xxxx>應用嗎?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show];
}
注: 另外說明一下
例如:相互跳轉的時候雙方都要設置URL與白名單 ,若是 FirstAPP 不設置URL types 項(自己注冊自己URL)
則實現的功能是:FirstAPP 可以跳轉到 SecondAPP ,但SecondAPP無法跳轉過來
當然雙方只設置 LSApplicationQueriesSchemes 項也是不行的,會提示應用程序未安裝 (白名單)
簡單說來 就是需要有一個要設置 URL
自己設置了的話,就是說已經有了URL,別人不注冊, 使用設置白名單后也能跳轉
總結:誰要跳,誰就要設置誰為白名單。 白名單要與跳到App設置的域名URL 要保持一致 另外代碼部分的URL也要以域名URL打頭即可