2012-08-08 72 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 - 我不嘗試在模擬器)

相關問題