使用MFMailComposeViewController在应用

使用MFMailComposeViewController在应用

本文介绍了无法使用MFMailComposeViewController在应用程序中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app which uses MFMailComposeViewController to send documents through email. I read somewhere that I need to enable at least one email so that the method [MFMailComposeViewController canSendEmail] will return YES and email the document. However, whenever I tap the email button, all it does is return to the previous view.

我检查了代码,[MFMailComposeViewController canSendEmail]返回否.谁能告诉我为什么会这样吗?

I checked the code and [MFMailComposeViewController canSendEmail] returns NO. Can anyone tell me why is this happening?

这是代码:

- (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框架

First add and Import the MessageUI Framework

#import <MessageUI/MessageUI.h>

并声明MFMaileComposeViewControllerDelegate

and Declare the 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的委托方法

and also write the delegate method of 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];
}

这篇关于无法使用MFMailComposeViewController在应用程序中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:32