首页 技术 正文
技术 2022年11月12日
0 收藏 670 点赞 3,112 浏览 1740 个字

iOS实现发送电子邮件的方法很简单,首先导入MessageUI.framework框架,然后代码如下:

 #import "RPViewController.h" //添加邮件头文件
#import <MessageUI/MFMailComposeViewController.h> @interface RPViewController () <MFMailComposeViewControllerDelegate> @end @implementation RPViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)click:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; //在这里可以设定邮件的默认标题/内容,也可以设置收件人等
[picker setSubject:@"标题"];
NSString *emailBody = @"邮件内容"; [picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
// [picker release];
}
else {
//无法发送邮件,在这里给用户提示
}
} #pragma mark 邮件代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
//取消发送
break;
case MFMailComposeResultSaved:
//保存草稿
break;
case MFMailComposeResultSent:
//发送成功
break;
case MFMailComposeResultFailed:
//发送失败
break;
default:
break;
} [self dismissViewControllerAnimated:YES completion:nil];
} @end

首先导入邮件的头文件,之后创建一个发送邮件用的控制器,设置默认参数,然后弹出这个控制器。

令当前控制器充当邮件控制器的代理,这样一来在邮件相关事件执行之后,就能给出一些用户提示了,也要在这里收起邮件视图。

发短信的道理完全一样,只是控制器换成了MFMessageComposeViewController,代理换成了MFMessageComposeViewControllerDelegate,配置代码如下:

     if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *message = [[MFMessageComposeViewController alloc] init];
message.messageComposeDelegate = self; message.recipients = @[@"电话号码"];
message.body = @"内容"; [self presentViewController:message animated:YES completion:nil];
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291