2

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

2 回答 2

6

首先添加并导入 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];
}
于 2012-08-08T11:54:35.647 回答
5

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

于 2012-08-08T11:51:28.050 回答