发送邮件功能开发步骤:
添加库MessageUI.framework
以简括号的方式,导入头文件 MessageUI/MessageUI.h,并且在定义类时,添加协议 MFMailComposeViewControllerDelegate代码如下:
#import <MessageUI/MessageUI.h>
LYGPopoverTableViewController : UITableViewController<MFMailComposeViewControllerDelegate>
判断设备是否支持发送邮件功能和是否进行了发送邮件功能的配置
//判断是否支持发送邮件
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
//检测当前设备是否配置了发送邮件功能,注意:canSendMail是静态方法,只能由类名调用,不能使用对象调用
if ([MFMailComposeViewController canSendMail])
{
//发送邮件
[self performSelectorOnMainThread:@selector(displayComposerSheet) withObject:nil waitUntilDone:YES];
}
else
{
}
}
else
{
}
-(void)displayComposerSheet
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//设置主题
[mailPicker setSubject: @"eMail主题"];
// 添加发送者
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailPicker setToRecipients: toRecipients];
// 添加图片
UIImage *addPic = [UIImage imageNamed: @"10.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
// NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"10.png"];
NSString *emailBody = @"eMail 正文";
[mailPicker setMessageBody:emailBody isHTML:YES];
// [self presentModalViewController: mailPicker animated:YES];//注意:ios6以上版本已经由一下方法代替
[self presentViewController:mailPicker animated:YES completion:^{
}];
// mailPicker = nil;
}