2.引入头文件
3.实现代理<MFMailComposeViewControllerDelegate> 和 <UINavigationControllerDelegate>
@代码示例:
- - (void)didClickSendEmailButtonAction{
- if ([MFMailComposeViewController canSendMail] == YES) {
- MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
- // 设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步)
- mailVC.mailComposeDelegate = self;
- // 收件人
- NSArray *sendToPerson = @[@"[email protected]"];
- [mailVC setToRecipients:sendToPerson];
- // 抄送
- NSArray *copyToPerson = @[@"[email protected]"];
- [mailVC setCcRecipients:copyToPerson];
- // 密送
- NSArray *secretToPerson = @[@"[email protected]"];
- [mailVC setBccRecipients:secretToPerson];
- // 主题
- [mailVC setSubject:@"hello world"];
- [self presentViewController:mailVC animated:YES completion:nil];
- [mailVC setMessageBody:@"魑魅魍魉,哈哈呵呵嘿嘿霍霍" ishtml:NO];
- }else{
- NSLog(@"此设备不支持邮件发送");
- }
- }
- - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
- switch (result) {
- case MFMailComposeResultCancelled:
- NSLog(@"取消发送");
- break;
- case MFMailComposeResultFailed:
- NSLog(@"发送失败");
- break;
- case MFMailComposeResultSaved:
- NSLog(@"保存草稿文件");
- break;
- case MFMailComposeResultSent:
- NSLog(@"发送成功");
- break;
- default:
- break;
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- // 系统发送,模拟器不支持,要用真机测试
- - (void)didClickSendSystemEmailButtonAction{
- NSURL *url = [NSURL URLWithString:@"[email protected]"];
- if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
- [[UIApplication sharedApplication] openURL:url];
- }else{
- NSLog(@"此设备不支持");
- }
- }
来源: http://www.phpxs.com/code/1005521/