1、首先咱们建立两个用于测试的App项目 (我这里以App0-A 和 App-B 为例)app
2、打开工程,设置工程的InfoPlist:添加URL Typeside
给你的App设置一个URL Schemes(明明以你的App或者工程名来命名) 这样就能让其它应用识别获得App测试
ps:咱们这里用App_B 去 handle 咱们的App_A,故咱们App_A就要设置URL Schemesurl
3、在App_B中,设置一个按钮,实现点击后handle出咱们的App_Aspa
- (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之间的跳转的功能了。code
注意:打开应用App-A的过程当中,App-A有两种状态。orm
第一种状态:App_A并无启动,那么会启动App_A。并调用下面的方法。blog
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; }
第二种状态:此时B已经启动了,可是在后台运行,这个时候不会调用该方法ci
4、若想实现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; }