APP拨打电话&发短信&发邮件

本文介绍了在iOS移动开发中如何实现拨打电话、发送短信和发送邮件的功能。提供了多种实现方式,包括使用UIWebView、MFMailComposeViewController和MFMessageComposeViewController等,详细解释了每种方法的优缺点以及使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在移动开发中我们会经常需要调用手机的拨打电话功能、发短信功能和发邮件功能,以下是我总结的方法:
//1.打电话
//方法1   最常用方法
NSURL *telUrl = [NSURL URLWithString:"tel://13161906451"];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
[webView loadRequest:[NSURLRequest requestWithURL:telUrl]];

//方法2   弊端:这个方法打电话结束后停留在拨号界面
NSURL *telUrl = [NSURL URLWithString:"tel://13161906451"];
[[UIApplication sharedApplication] openURL:telUrl];

//方法3   拨打电话之前会询问用户是否拨打,挂断电话之后会返回应用
#warn 不能用,审核不通过,私有API
NSURL *telUrl2 = [NSURL URLWithString:"telprompt://13161906451"];
[[UIApplication sharedApplication] openURL:telUrl2];


//2.发邮件
//方法1
//如果想指定邮件内容,那就得使用MessageUI框架
//包含主头文件
#import <MessageUI/MessageUI.h>
// 不能发邮件
if (![MFMailComposeViewController canSendMail]) return;

// 当邮件发送成功或者失败或者取消之后会回到原始程序
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];

// 设置邮件主题
[vc setSubject:@"会议"];
// 设置邮件内容
[vc setMessageBody:@"今天下午开会吧" isHTML:NO];
// 设置收件人列表
[vc setToRecipients:@[@"643055866@qq.com"]];
// 设置抄送人列表
[vc setCcRecipients:@[@"1234@qq.com"]];
// 设置密送人列表
[vc setBccRecipients:@[@"56789@qq.com"]];

// 添加附件(一张图片)
UIImage *image = [UIImage imageNamed:@"lufy.jpeg"];
NSData *data = UIImageJPEGRepresentation(image, 0.5);
//去百度上搜mimeType
[vc addAttachmentData:data mimeType:@"image/jepg" fileName:@"lufy.jpeg"];

// 设置代理
vc.mailComposeDelegate = self;
// 显示控制器
[self presentViewController];

//邮件发送后的代理方法回调,发完后会自动回到原应用
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    // 关闭邮件界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    if (result == MFMailComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MFMailComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}


//方法2    弊端:不会返回应用程序
NSURL *emailUrl = [NSURL URLWithString:"mailto://1032172045@qq.com"];
[[UIApplication sharedApplication] openURL:emailUrl];



//3.发短信
//方法1
//如果想指定短信内容,那就得使用MessageUI框架
//包含主头文件
#import <MessageUI/MessageUI.h>
//如果不能发送直接返回,模拟器不能发短信,调用发短信会崩溃
if(![MFMessageComposeViewController canSendText]) return;
//显示发短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"吃饭了没?";
// 设置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 设置代理,这个代理会使右上角出现取消按钮
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];

//代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    if (result == MessageComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}

// 显示控制器
[self presentViewController:vc animated:YES completion:nil];

//方法2   弊端:发送结束后不能返回到应用
NSURL *smslUrl = [NSURL URLWithString:"sms://13161906451"];
[[UIApplication sharedApplication] openURL:smslUrl];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值