-
MFMailComposeViewControl
ler
MFMailComposeViewControl
- 1.项目中引入MessageUI.framework;
-
2.在使用的文件中导入MFMailComposeViewControl
ler.h头文件; -
3.实现MFMailComposeViewControl
lerDelegate,处理邮件发送事件; -
4.调出邮件发送窗口前先使用MFMailComposeViewControl
ler里的“+ (BOOL)canSendMail”方法检查用户是否设置了邮件账户; -
5.初始化MFMailComposeViewControl
ler,构造邮件体
-
#import
-
#import
-
@interface
ViewController : UIViewController -
@end
-
#pragma
mark - 在应用内发送邮件 -
//激活邮件功能
-
-
(void)sendMailInApp -
{
-
Class mailClass = (NSClassFromString(@"MFMailComposeViewControl ler")); -
if (!mailClass) { -
[self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"]; -
return; -
} -
if (![mailClass canSendMail]) { -
[self alertWithMessage:@"用户没有设置邮件账户"]; -
return; -
} -
[self displayMailPicker]; -
}
-
-
//调出邮件发送窗口
-
-
(void)displayMailPicker -
{
-
MFMailComposeViewControl ler *mailPicker = [[MFMailComposeViewControl ler alloc] init]; -
mailPicker.mailComposeDelegate = self; -
//设置主题 -
[mailPicker setSubject: @"eMail主题"]; -
//添加收件人 -
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"]; -
[mailPicker setToRecipients: toRecipients]; -
//添加抄送 -
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; -
[mailPicker setCcRecipients:ccRecipients]; -
//添加密送 -
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil]; -
[mailPicker setBccRecipients:bccRecipients]; -
-
// 添加一张图片 -
UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"]; -
NSData *imageData = UIImagePNGRepresentation (addPic); // png -
//关于mimeType:http://www.iana.org/assignments/media-types/index.html -
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"]; -
-
//添加一个pdf附件 -
NSString *file = [self fullBundlePathFromRelati vePath:@"高质量C++编程指南.pdf"]; -
NSData *pdf = [NSData dataWithContentsOfFile:file]; -
[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"]; -
//添加一个视频 -
NSString *path=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20121219.avi"]; NSData *video = [NSData dataWithContentsOfFile:path]; [mailPicker addAttachmentData:video mimeType: @"" fileName:@"20121219.avi"]; -
NSString *emailBody = @"eMail 正文"; -
[mailPicker setMessageBody:emailBody isHTML:YES]; -
[self presentModalViewControll er: mailPicker animated:YES]; -
[mailPicker release]; -
}
-
#pragma
mark - 实现 MFMailComposeViewControl lerDelegate -
-
(void)mailComposeController:(MFMailComposeViewControl ler *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error -
{
//关闭邮件发送窗口 -
[self dismissModalViewControll erAnimated:YES]; -
NSString *msg; -
switch (result) { -
case MFMailComposeResultCance lled: -
msg = @"用户取消编辑邮件"; -
break; -
case MFMailComposeResultSaved : -
msg = @"用户成功保存邮件"; -
break; -
case MFMailComposeResultSent: -
msg = @"用户点击发送,将邮件放到队列中,还没发送"; -
break; -
case MFMailComposeResultFaile d: -
msg = @"用户试图保存或者发送邮件失败"; -
break; -
default: -
msg = @""; -
break; -
} -
[self alertWithMessage:msg]; -
}