ios 调用系统应用的方法 应用间跳转的方法

声明一个私有方法:

#pragma mark - 私有方法
-(void)openUrl:(NSString *)urlStr{
    //注意url中包含协议名称,iOS根据协议确定调用哪个应用,例如发送邮件是“sms://”其中“//”可以省略写成“sms:”(其他协议也是如此)
    NSURL *url=[NSURL URLWithString:urlStr];
    UIApplication *application=[UIApplication sharedApplication];
    if(![application canOpenURL:url]){
        NSLog(@"无法打开\"%@\",请确保此应用已经正确安装.",url);
        return;
    }
    [[UIApplication sharedApplication] openURL:url];
}

调用系统应用的方法:

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.backgroundColor = [UIColor blueColor];
    [button2 setTitle:@"风好大" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button2.titleLabel.textAlignment = NSTextAlignmentCenter;
    button2.frame = CGRectMake(100, 360, 100, 50);
    [button2 addTarget:self action:@selector(BtnClickTwo:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

-(void)BtnClickTwo:(id)sender
{
    //打电话
    /*
     NSString *phoneNumber = @"13592671307";
     NSString *url = [NSString stringWithFormat:@"telprompt://%@",phoneNumber];
     [self openUrl:url];
     */

    //发送短信
    /*
     NSString *phoneNumber = @"13592671307";
     NSString *url = [NSString stringWithFormat:@"sms://%@",phoneNumber];
     [self openUrl:url];
     */

    //发送邮件
    /*
     NSString *mailAddress = @"303265997@qq.com";
     NSString *url = [NSString stringWithFormat:@"mailto://%@",mailAddress];
     [self openUrl:url];
     */

    //浏览网页
    /*
     NSString *url = @"https:www.baidu.com";
     [self openUrl:url];
     */

}

这样就可以直接应用了,当然了,打电话和发短信都是可以返回本应用的!如果大家有其他的需求可以自行百度!

 

拓展篇:应用间跳转

在有些时候,我们会遇到这样的需求,在一个应用中需要打开其他应用,那么这个效果是怎么实现呢:

1,创建2个应用

2,在被打开的应用中,设置练习2的url;

 

3,在练习中编写打开练习2的代码:

-(void)BtnClickTwo:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"appb:"];
    [[UIApplication sharedApplication]openURL:url];
}

 

备注:有两种状态

第一种状态:练习2没有打开,那么会启动练习2,并调用入口类的方法

第二种状态:练习2在后台运行,那么不会调用入口类方法

 

拓展:

当一个应用被另一个应用打开的时候,会调用以下方法:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

在该方法中可以实现两个应用程序间的数据局传递

posted @ 2016-05-20 14:50  稻草人11223  阅读(329)  评论(0编辑  收藏  举报
返回顶部