一、首先我們創建兩個用於測試的App項目 (我這里以App0-A 和 App-B 為例)
二、打開工程,設置工程的InfoPlist:添加URL Types
給你的App設置一個URL Schemes(明明以你的App或者工程名來命名) 這樣就能讓其它應用識別得到App
ps:我們這里用App_B 去 handle 我們的App_A,故我們App_A就要設置URL Schemes
三、在App_B中,設置一個按鈕,實現點擊后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]; }
點擊按鈕后:
這樣就能實現App之間的跳轉的功能了。
注意:打開應用App-A的過程中,App-A有兩種狀態。
第一種狀態:App_A並沒有啟動,那么會啟動App_A。並調用下面的方法。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; }
第二種狀態:此時B已經啟動了,但是在后台運行,這個時候不會調用該方法
四、若想實現App跳轉的同時進行傳值,只需實現application的代理方法
//當應用程序被其他程序打開的時候會調用這個方法,在該方法中可以實現兩個應用程序間的數據局傳遞
//通過這個代理方法可以攔截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:@""];//參數就在url,傳值也在里面 } return NO; }