2012-08-08 93 views
1

我有一个应用程序使用MFMailComposeViewController通过电子邮件发送文档。我在某处阅读我需要启用至少一封电子邮件,以便方法[MFMailComposeViewController canSendEmail]将返回YES并通过电子邮件发送文档。然而,只要我点击电子邮件按钮,它所做的就是返回到前一个视图。使用MFMailComposeViewController无法在应用程序中发送电子邮件

我查了代码,[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]; 
     } 
} 

回答

5

首先添加和导入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:@"[email protected]", @"[email protected]", 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]; 
} 
+0

喜的委托方法!一旦我尝试了这个,我会尽快回复你。感谢您的回应! :) – 2012-08-08 14:20:09

+0

这一个工程。谢谢! – 2012-08-09 02:51:56

+0

提示信息应该是“为了邮件,请在设置中配置您的电子邮件帐户。” – 2015-08-13 07:45:24

5

因为iPhone的邮件应用程序不权威性。转到首选项 - >邮件(或简单地打开邮件应用)和谷歌或其他服务的身份验证,你可以通过MFMailComposeViewController发送电子邮件。 (这在真实iPhone - 我不尝试在模拟器)

相关问题