xcode 如何使用 MFMailComposeViewController 设置收件人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9063760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:09:05  来源:igfitidea点击:

How to setToRecipients with MFMailComposeViewController

iphoneobjective-cxcodemfmailcomposeviewcontroller

提问by user1177647

I'm trying to get emails that i select to send an email. But i dont know how to setToRecipients which users that i heve selected in the MFMailComposeViewController view.

我正在尝试接收我选择发送电子邮件的电子邮件。但我不知道如何设置我在 MFMailComposeViewController 视图中选择的用户的接收者。

if ([MFMailComposeViewController canSendMail])
    {
        mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"A Message from blablabl"];

        NSMutableArray *usersTo = [[NSMutableArray alloc] init];
        toRecipients = usersTo;
        [mailer setToRecipients:toRecipients];

        NSString *emailBody = @"blablablabal";

        [mailer setMessageBody:emailBody isHTML:YES];

        // only for iPad
        // mailer.modalPresentationStyle = UIModalPresentationPageSheet;

        [self presentModalViewController:mailer animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                                                        message:@"Your device doesn't support the composer sheet" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
        [alert show];
    }

Delegate http://pastie.org/3281814

代表 http://pastie.org/3281814

回答by Michael Dautermann

A couple things are wrong here.

这里有几件事是错误的。

1 )

1)

MFMailComposeViewController's setToRecipientsmethod expects an immutable array with recipients already set.

MFMailComposeViewController 的setToRecipients方法需要一个已设置收件人的不可变数组。

2 )

2)

And you're setting it to a blank mutable array.

你将它设置为一个空白的可变数组。

Try something like this:

尝试这样的事情:

NSArray *usersTo = [NSArray arrayWithObject: @"[email protected]"];
[mailer setToRecipients:usersTo];

And you should see it work.

你应该看到它起作用了。

回答by Tendulkar

   MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
   NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",nil];   
   [picker setToRecipients:toRecipients];

回答by N.Rakesh

MFMailComposer In “ Swift “ and “ Objective c “
*********************************************
In objective c
Steps:
1. Create new project.
2. Add a button into storyboard.
3. In .h add header file  like “Import <MessageUI/MessageUI.h>”
4. Add delegate to ViewController “ MFMailComposeViewControllerDelegate”

5. In Button Action     {
                            NSString *emailTitle  = “”
                            NSString *messageBody = “”
                            NSArray *toRecipents  = “”                                  
                            MFMailComposeViewController *VC = [[MFMailComposeViewController]alloc init];
                            VC.mailComposeDelegate = self;
                            [VC.setSubject: emailTitle];
                            [VC.setMessageBody: messageBody];
                            [VC.setToRecepents: toRecipents];
           if([MFMailComposeViewController canSendMail]) {
              [self presentViewController:mc animated:YES completion:NULL];
           }

6.MailComposeController Function

       - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

7. And also add  FrameWork: MessageUI.FrameWork

8. Run the App into mobile or Simulator.    




In Swift:
*******
Steps:
1. Create New Project 

2. Add a button Storyboard

3. In ViewController.Swift Class File   Import MessageUI

4. In Button Action  ConfiguredMailComposer below Steps  
      {
        let mailComposeViewController = configuredMailComposeViewController()

        if MFMailComposeViewController.canSendMail() 
{
            self.present(mailComposeViewController, animated: true, completion: nil)
            self.showSendmailSuccesfulAlert()
        } else {

            self.showSendMailErrorAlert()  

        }
5. Implement the configure mail composer 

      func configuredMailComposeViewController() -> MFMailComposeViewController {

        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self 
        mailComposerVC.setToRecipients(["[email protected]"])
        mailComposerVC.setSubject("Sending you an in-app e-mail...")
        mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
        return mailComposerVC

    }

6. MailComposer optional method

        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

        controller.dismiss(animated: true, completion: nil)

    }

7. After completed the step no: run the app into device or simulator.