您的位置:首页 > 移动开发

通过openURL的方式启动其它App

2014-06-18 16:15 337 查看
假设有两个App,项目名分别是SampleA和SampleB,需要在SampleA里点击一个Button来启动SampleB,并传递一个字符串。具体实现步骤如下:

1. 在SampleB的info.plist文件里新增一个URL Schemes,并指定一个字符串,这个字符串就是调用App的链接名称:



2. 在SampleA的按钮点击操作里执行下面代码:

- (IBAction)openClickHandler:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myapp23://time_is_money"]];
}


3. 在SampleB的AppDelegate类里,调用系统事件处理传递过来的URL:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSString *strInfo = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:strInfo delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

return YES;
}


总结:

1. 通过openURL的方式启动其它App时,地址格式为 @“URL Scheme://URL identifier”,其中URL identifier是可选的。

2. URL identifier不能含有空格和问号。

3. apple并不支持这种做法,实际产品开发中慎用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: