一、首先我們創(chuàng)建兩個用于測試的App項目(我這里以App-A 和 App-B 為例)

image

image
二、打開工程,設(shè)置工程的InfoPlist:添加URL Types
給你的App設(shè)置一個URL Schemes(明明以你的App或者工程名來命名) 這樣就能讓其它應(yīng)用識別得到App
ps:我們這里用App_B 去 handle 我們的App_A,故我們App_A就要設(shè)置URL Schemes

image
三、在App_B中,設(shè)置一個按鈕,實現(xiàn)點擊后handle出我們的App_A
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *App_B_Button = [UIButton buttonWithType:UIButtonTypeCustom];
App_B_Button.frame = CGRectMake(100,100,100,50);
App_B_Button.backgroundColor = [UIColor purpleColor];
[App_B_Button setTitle:@"App_B" forState:UIControlStateNormal];
[App_B_Button addTarget:self action:@selector(app_B:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:App_B_Button];
}
-(void)app_B:(UIButton *)buttonB {
NSURL *url = [NSURL URLWithString:@"appA://"];
[[UIApplication sharedApplication] openURL:url];
}
點擊按鈕后:

image
這樣就能實現(xiàn)App之間的跳轉(zhuǎn)的功能了。
注意:打開應(yīng)用App-A的過程中,App-A有兩種狀態(tài)。
第一種狀態(tài):App_A并沒有啟動,那么會啟動App_A。并調(diào)用下面的方法。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
第二種狀態(tài):此時B已經(jīng)啟動了,但是在后臺運行,這個時候不會調(diào)用該方法
四、若想實現(xiàn)App跳轉(zhuǎn)的同時進行傳值,只需實現(xiàn)application的代理方法
//當(dāng)應(yīng)用程序被其他程序打開的時候會調(diào)用這個方法,在該方法中可以實現(xiàn)兩個應(yīng)用程序間的數(shù)據(jù)局傳遞
//通過這個代理方法可以攔截url
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSString *urlStr = [url absoluteString];
if([urlStr hasPrefix:@"AppA://"]) {
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"AppA://"withString:@""];//參數(shù)就在url,傳值也在里面
}
return NO;
}