调用第三方应用

在手机QQ里,QQ每天都会给你推送新闻,当你想看更多新闻的时候,新闻的最下方会有“打开腾讯新闻,了解更多”的按扭,当你点击之后会跳到腾讯新闻的应用,前提是你已经安装了这两个应用。那么,这个是怎样实现的呢?

 

我们需要在应用A跳转到应用B,首先要确保应用B是否配置了URL types,只有配置了URL types属性才允许应用被跳转。

 

应用B:

修改info.plist文件:

默认是没有URL types这个节点的,我们新建一个URL types节点,在URL types节点里的Item0节点新建一个URL Schemes节点,整体结构如下所示。

URL Schemes->Item0是必填的,作为跳转URL的协议。URL identifier是选填的。

 

应用A:

打开第三方应用代码:

NSString *aUrl = @"GC://params";
NSURL *url = [NSURL URLWithString:aUrl];
[[UIApplication sharedApplication] openURL:url];

url后面可以传参数,在被跳转的应用接收。 

 

应用B:

我们可以使用application:openURL:sourceApplication:annotation:方法接收应用间调用的参数:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    // 接收参数
    NSMutableString *str = [NSMutableString string];
    [str appendString:[NSString stringWithFormat:@"url - %@\n", url]];
    [str appendString:[NSString stringWithFormat:@"Application Bundle Identifier - %@\n", sourceApplication]];
    [str appendString:[NSString stringWithFormat:@"params - %@", [url host]]];
    NSLog(@"%@", str);
    return YES;//是否打开
}

 

posted @ 2015-01-29 09:11  GarveyCalvin  阅读(299)  评论(0编辑  收藏  举报