【问题标题】:How to setToRecipients with MFMailComposeViewController如何使用 MFMailComposeViewController 设置收件人
【发布时间】:2012-01-30 12:18:27
【问题描述】:

我正在尝试获取我选择发送电子邮件的电子邮件。 但我不知道如何设置我在 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];
    }

代表 http://pastie.org/3281814

【问题讨论】:

    标签: iphone objective-c xcode mfmailcomposeviewcontroller


    【解决方案1】:

    这里有几件事是错误的。

    1)

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

    2)

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

    试试这样的:

    NSArray *usersTo = [NSArray arrayWithObject: @"nobody@stackoverflow.com"];
    [mailer setToRecipients:usersTo];
    

    你应该会看到它起作用了。

    【讨论】:

    • 它不起作用....“nobody@stackoverflow.com & 1 more\U2026”,只能收到一封电子邮件,因为在他放 & n 个用户之后...我要尝试调整 uitextfield 的大小
    • @Michael Dautermann 使用 IOS 模拟器在 Xcode 中测试应用程序时无法发送电子邮件,因为 IOS 模拟器的设置中缺少邮件应用程序。当我将 Iphone 设备连接到我的 Mac 并在此设备上从 Xcode 运行应用程序时,有没有办法在 Mac 上显示我的 Iphone 屏幕,以便我可以控制在物理设备上运行的应用程序鼠标并从 Mac 上显示的屏幕发送电子邮件,而不是触摸 Iphone 设备本身的屏幕?非常感谢
    • 任何 API 使预定义的收件人只读?这样用户就不能添加新的收件人了?
    • @SazzadHissainKhan 我不知道! Apple 可能更希望用户能够控制代表用户发送电子邮件的位置。
    【解决方案2】:
       MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
       NSArray *toRecipients = [NSArray arrayWithObjects:@"first@gmail.com",@"second@gmail.com",nil];   
       [picker setToRecipients:toRecipients];
    

    【讨论】:

      【解决方案3】:
      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(["someone@somewhere.com"])
              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.
      

      【讨论】:

        猜你喜欢
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多