【原+转】iOS一个应用打开另一个应用
2017-08-01 本文已影响33人
紫荆逸
实现效果:应用app1的一个按钮点击,进入应用app2应用。
主要代码:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app2://"]];
具体步骤如下:
1.先给app2添加一个URL Schemes:app2.
1.app2添加URL Schemes.png3.app1代码:找到info.plist,添加LSApplicationQueriesSchemes,添加app2的URL Schemes,这句话很重要!!:
2.给app1的info.plist添加app2的url schemes.png3.给app1的按钮点击事件写上这句话即可:
- (IBAction)ClickOpenUrlAction:(id)sender {
//打开app2:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app2://"]];
}
4.先运行app2,再运行app1,点击app1的按钮,就可以打开app2的应用了。
【拓展】:
如果想要打开app2的时候给app2传递参数,可以在“// ”后面写上,如: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app2://id=22&&name=mmmm"]];
这样就实现了第一步打开要调用的应用,app2://就是本地的url,后面的参数则是你要传递的参数,这是为了后面打开应用中指定的页面做准备。
1.app1项目打开app2应用:
//打开app2:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app2://id=22&&name=mmm"]];
2.app2项目的appdelete.m文件代码接收参数:
//应用未在后台执行走这里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (launchOptions!=nil){
[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if(url)
{
NSString *urlString=[NSString stringWithFormat:@"%@",url];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"url"];
}
}
return YES;
}
//应用后台运行的话会走这里
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
NSLog(@"输出%@",url);
NSString *urlString=[NSString stringWithFormat:@"%@",url];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"url"];
return YES;
}
//应用后台运行的话会走这里
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSLog(@"ddd输出%@",url);
NSString *urlString=[NSString stringWithFormat:@"%@",url];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"url"];
return YES;
}
如图:
app2接收参数.png参考:
1.http://blog.csdn.net/lh_work/article/details/25197781
2.http://www.cnblogs.com/-zhangwy/p/6026040.html