现在的位置: 首页 > 综合 > 正文

[iOS]通过openURL启动第三方APP传参

2013年10月17日 ⁄ 综合 ⁄ 共 1109字 ⁄ 字号 评论关闭

=====工程A=====

0. 建立工程A, 先调出URL Types

1.Add Row 一个URL Schemes 并随便起个名字 (这就是调用这个app的唯一链接)

2. 在工程A的AppDelegate.m里加入以下系统方法:

 (这个方法会捕获调用本工程的程序传递过来的URL identifier文本)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-
(
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];
    [alertViewshow];
    [alertViewrelease];
    return YES;
}

 

====工程B=====

3. 建立工程B, 添加调用语句:

1
[[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禁止的功能, 所以请谨慎使用, 但如果是客户问"设备能力"这方面的问题, 那么答案是"可以",  但 不建议那么做. 

抱歉!评论已关闭.