IOS网友们的篇章iOS开发Ios@IONIC

iOS在自己的app里面如何打开其他app,例如QQ/微信等.m

2016-06-23  本文已影响3919人  shannoon

0. 想要打开其他APP,需要知道目标APP的URL Scheme,然后调用UIApplication的 openURL:方法即可打开


    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];

1. 配置Scheme白名单

untitled2.png

2. 常用URL Scheme

3. 示例代码


- (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://"]];
}
上一篇 下一篇

猜你喜欢

热点阅读