无法使用MFMailComposeViewController在应用程序中发送电子邮件

时间:2012-08-08 11:27:54

标签: ios mfmailcomposeviewcontroller

我有一个使用MFMailComposeViewController通过电子邮件发送文档的应用。我在某处读到了我需要启用至少一封电子邮件,以便方法[MFMailComposeViewController canSendEmail]将返回YES并通过电子邮件发送文档。但是,每当我点击电子邮件按钮时,它只会返回上一个视图。

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

2 个答案:

答案 0 :(得分:5)

因为您在iphone中的邮件应用程序无法验证。转到首选项 - >从谷歌或其他服务邮件(或只是打开邮件应用程序)和身份验证,您可以通过MFMailComposeViewController发送电子邮件。 (这在真正的iPhone上 - 我不在模拟器上试试)

答案 1 :(得分:5)

首先添加并导入MessageUI Framework

#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];
}