ios打电话、短信、邮件

来源:互联网 发布:免费杀木马软件 编辑:程序博客网 时间:2024/05/18 04:00

在APP开发中,可能会涉及到打电话、发短信、发邮件等功能。

1.拨打电话有三种方式:

第一种1.1: 直接拨打,跳转到拨号界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@”tel://%@”, @“你所需要拨打的电话号码”]]];
第二种1.2: 提示拨打(苹果原生自带)
拨号之前会弹框询问,打完电话后能自动回到原应用。

提示拨打电话效果图.png
1.2.1、 创建一个UIWebView来加载URL,拨完后能自动回到原应用。建议使用这种方案!

// 提示拨打方法1:
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@”tel:%@”, @”你所需要拨打的电话号码”];
UIWebView * callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
1.2.2、 缺点:私有API,因此可能通不过苹果官方审核。如果是企业级应用(不需要上线appStore),可以使用这个方法。

// 提示拨打方法2:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@”telprompt:%@”, @”你所需要拨打的电话号码”]];
// 调用openURL:
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
第三种1.3: 弹框提示拨打(根据UI设计界面修改)
需要封装自定义弹框文件: QTXAlterView

@property (nonatomic, strong) QTXAlterView *telAlterView; // 拨打电话的弹框

QTXAlterView *alter = [[QTXAlterView alloc] initWithMessage:[NSString stringWithFormat:@”你所需要拨打的电话号码”] delegate:self rightButtonTitle:@”确定” otherButtonTitles:@”取消”];
[alter show];
self.telAlterView = alter;

pragma mark - QTXAlterViewDelegate

  • (void)alertView:(QTXAlterView *)alertView clickedAtIndex:(NSInteger)buttonIndex {
    if (alertView == self.telAlterView) { // 拨打电话
    if (buttonIndex == 1) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@”tel://%@”, @“你所需要拨打的电话号码”]]];
    }
    }
    }
    2.发短信的方式有两种:

(1)直接跳转到发短信界面。
缺点:不能定义发送短信的内容,且发完短信后不能自动回到原应用。

NSURL *url = [NSURL URLWithString:@”sms://10010”];[[UIApplication sharedApplication] openURL:url];
(2)使用MessageUI 框架发送短信, 建议使用第二种方法。

需要包含头文件 #import