在项目开发中,很多时候会遇到在自己应用中调用其他APP的情况, 下面做简单记录总结.
第一步:
新建一个工程后为其添加 添加URL Types项
打开项目中info.plist文件,在infomation property list项下面增加一项URL Typs
如图分为自身的URL Scheme .
下面新建的LSApplicationQueriesSchemes 是允许跳转的APP的URL .这里必须建立.
这里示例中是两个应用互相唤醒的, 实际项目中基本都是自身应用掉其他的应用,因为你不可能把自己的URL Scheme写进微信里去吧 哈哈,返回都是自己写好的.
贴出主要代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
//06.13 测试APP跳转
UIButton * TEBtn = [[UIButton alloc]init];
TEBtn.frame = CGRectMake(20, 0, 160, 50);
TEBtn.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
[TEBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[TEBtn setBackgroundColor:[UIColor redColor]];
[TEBtn addTarget:self action:@selector(jumpToAPP2) forControlEvents:UIControlEventTouchUpInside];
[TEBtn setTitle:@"测试跳转1" forState:UIControlStateNormal];
TEBtn.layer.cornerRadius = 5;
TEBtn.layer.masksToBounds = YES;
[self.view addSubview:TEBtn];
}
跳转实现方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”testapp2://formapp1”]];
通过修改 testapp2:// 可以跳转到不同的指定应用.
//06.13 测试跳转
-(void)jumpToAPP2
{
NSLog(@"点击了");
if([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"testapp2://"]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"testapp2://formapp1"]];
}
}
后面testapp2:// 之后的 formapp1 为跳转是所传递的参数.
可以在跳转到的应用的AppDelegate.m中接受数据.
方法实现如下:
//跳转后调用接受参数(若无参数使用可注释不用)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *receText = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"参数:%@ URL:%@",receText, url.absoluteString);
return YES;
}
打印如下:
2016-06-14 15:44:36.803 TestAPP1[8840:67444] 点击了
2016-06-14 15:44:44.977 TestAPP1[8840:67444] 参数:formapp2 URL:testapp1://formapp2
相当方便简单.
附demo地址:http://code.cocoachina.com/view/131498