通过openURL启动第三方app并传参数
转自:http://lifeinbeta.diandian.com/post/2012-05-31/40028982809
=====工程A=====
0. 建立工程A, 先调出URL Types
1.Add Row 一个URL Schemes 并随便起个名字 (这就是调用这个app的唯一链接)
2. 在工程A的AppDelegate.m里加入以下系统方法:
(这个方法会捕获调用本工程的程序传递过来的URL identifier文本)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url
{
// 处理传递过来的参数
UIAlertView *alertView;
NSString*text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
alertView = [[UIAlertView alloc] initWithTitle:@"Text"
message:text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
return YES;
}
{
// 处理传递过来的参数
UIAlertView *alertView;
NSString*text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
alertView = [[UIAlertView alloc] initWithTitle:@"Text"
message:text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
return YES;
}
====工程B=====
3. 建立工程B, 添加调用语句:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myapp://foo=444bar=222"]];
4. 编译运行工程B, 结果是:
B启动->A启动->A弹出Alert:
另外:
0. 从结果看出app的地址构成是: URL Scheme://URL identifier
1. 单用URL Scheme 即可打开程序, 即URL identifier是可选的
2. myapp://后面的字 可以为点”.”和等号”=” 不可以为空格和问号
3. stackoverflow上有很多人指出这是apple禁止的功能, 所以请谨慎使用, 但如果是客户问”设备能力”这方面的问题, 那么答案是”可以”, 但 不建议那么做.
参考阅读:
Launching Your Own Application via a Custom URL Scheme
iOS SDK: Working with URL Schemes
转载请注明: 转自Rainbird的个人博客
本文链接: 通过openURL启动第三方app并传参数