使用MFMailComposeViewController无法在应用程序中发送电子邮件
我有一个应用程序使用MFMailComposeViewController
通过电子邮件发送文件。 我读了一些地方,我需要至less启用一个电子邮件,以便方法[MFMailComposeViewController canSendEmail]
将返回YES并通过电子邮件发送文档。 但是,每当我点击电子邮件button,它所做的只是返回到前一个视图。
我检查了代码, [MFMailComposeViewController canSendEmail]
返回NO。 谁能告诉我为什么会发生这种情况?
这里是代码:
- (void)sendEmail { if ([MFMailComposeViewController canSendMail] == NO) return; NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName; NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil]; if (attachment != nil) { MFMailComposeViewController *mailComposer = [MFMailComposeViewController new]; [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName]; [mailComposer setSubject:fileName]; mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical; mailComposer.modalPresentationStyle = UIModalPresentationFormSheet; mailComposer.mailComposeDelegate = self; [self presentModalViewController:mailComposer animated:YES]; [mailComposer release]; } }
首先添加并导入MessageUI框架
#import <MessageUI/MessageUI.h>
并声明MFMaileComposeViewControllerDelegate
@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
写这个代码发送邮件
- (IBAction)openMail:(id)sender { if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; mailer.mailComposeDelegate = self; [mailer setSubject:@"xyz"]; NSArray *toRecipients = [NSArray arrayWithObjects:@"fisrtMail@example.com", @"secondMail@example.com", nil]; [mailer setToRecipients:toRecipients]; NSData *pdfdata = [NSData dataWithContentsOfURL:"Your URL"] [mailController addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"file.pdf"]; NSString *emailBody = @"xyz"; [mailer setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:mailer animated:YES]; [mailer release]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Your device doesn't support the composer sheet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [alert release]; }
}
同时也编写MFMailComposeViewControllerDelegate的委托方法
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { switch (result) { case MFMailComposeResultCancelled: NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); break; case MFMailComposeResultSaved: NSLog(@"Mail saved: you saved the email message in the drafts folder."); break; case MFMailComposeResultSent: NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); break; case MFMailComposeResultFailed: NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); break; default: NSLog(@"Mail not sent."); break; } // Remove the mail view [self dismissModalViewControllerAnimated:YES]; }
因为你在iphone的邮件应用程序不auth。 转到首选项 – >邮件(或简单地打开邮件应用程序)和谷歌或其他服务的身份validation,您可以通过MFMailComposeViewController发送电子邮件。 (这在真正的iPhone上 – 我不试试在模拟器上)