1、想要打开其他APP,需要知道目标APP的URL Scheme,然后调用UIApplication的 openURL:方法即可打开
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
2、 配置Scheme白名单
2.1、在iOS 9下涉及到平台客户端跳转,系统会自动到项目info.plist下检测是否设置平台Scheme。对于需要配置的平台,如果没有配置,就无法正常跳转平台客户端。因此要支持客户端的分享和授权等,需要配置Scheme名单
2.2、具体方法:
2.2.1、在项目的info.plist中添加一LSApplicationQueriesSchemes,类型为Array。
2.2.2、然后给它添加一个需要支持的项目,类型为字符串类型;
2.3、想要获取具体的各个平台的白名单,看第三方平台就可以,例如:
sharesdk分享 iOS9适配文档地址
友盟分享 iOS9适配地址
2.4、info.plist示例图
3、常用URL Scheme
常用URL scheme查询 http://handleopenurl.com/scheme
QQ: mqq://
微信: weixin://
新浪微博: weibo:// (sinaweibo://)
腾讯微博: tencentweibo://
4、示例代码
- (void)viewDidLoad {
[super viewDidLoad];
[self addweixinbutton];
[self addqqbutton];
}
- (void)addqqbutton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"打开QQ" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
button.frame = CGRectMake(100, 250, 100, 100);
[button addTarget:self action:@selector(openQQ) forControlEvents:UIControlEventTouchUpInside];
}
- (void)addweixinbutton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"打开微信" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(openWeixin) forControlEvents:UIControlEventTouchUpInside];
}
/**
* 需要在info里面添加 LSApplicationQueriesSchemes字段
*/
- (void)openQQ
{
NSURL *url = [NSURL URLWithString:@"mqq://"];
if([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
} else {
UIAlertView*ale=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有安装手机QQ,请安装手机QQ后重试,或用PC进行操作。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[ale show];
}
}
/**
* 打开微信 , 没有配置
*/
- (void)openWeixin
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
}