第一步:導入頭文件(即所使用的類庫)web
#import <MessageUI/MessageUI.h>工具
第二步:遵照協議spa
@interface class ()<MFMailComposeViewControllerDelegate>.net
第三步:實現分享方法和協議方法3d
1.實現分享方法orm
-(void)shareEmail{get
//判斷是否發送Emailstring
BOOL canSendMail=[MFMailComposeViewController canSendMail];it
if (canSendMail) {io
//建立郵件視圖控制器
MFMailComposeViewController *mailComposeViewController=[[MFMailComposeViewController alloc]init];
NSString *subjectString = [NSString stringWithFormat:@"[旅拍] %@",_webInfo.title];
NSString *bodyString = [NSString stringWithFormat:@"我製做了一個名爲\"%@\"的旅拍,趕快來欣賞吧!\n\n請猛戳連接:\n%@\n\n旅拍-最方便的拍照記錄分享工具",_webInfo.title,_webInfo.webUrl];
[mailComposeViewController setSubject:subjectString];
[mailComposeViewController setMessageBody:bodyString isHTML:NO];
[mailComposeViewController setMailComposeDelegate:self];
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
}
2.協議方法
#pragma mark - MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"取消發送郵件");
break;
case MFMailComposeResultSent:
NSLog(@"發送郵件成功");
break;
case MFMailComposeResultFailed:
NSLog(@"發送郵件失敗");
break;
case MFMailComposeResultSaved:
NSLog(@"發送郵件保存");
break;
default:
break;
}
}