【问题标题】:How to send mail from a custom UITableViewCell using MFMailComposerViewController [duplicate]如何使用 MFMailComposerViewController 从自定义 UITableViewCell 发送邮件 [重复]
【发布时间】:2025-11-25 03:40:01
【问题描述】:

我想在使用MFMailComposeViewController 的自定义UITableViewCell 中单击按钮时发送邮件。

如果答案在 Objective-C 中,请欣赏。

【问题讨论】:

    标签: ios objective-c mfmailcomposeviewcontroller


    【解决方案1】:

    .h 文件中的以下代码

    #import <MessageUI/MFMailComposeViewController.h>

    给代表<MFMailComposeViewControllerDelegate>

    .m 文件中的以下代码

    //Where you want to open dialog write below code
    
    if([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
            mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send
    
            [mailCont setSubject:@"Your Subject!"];
            [mailCont setToRecipients:[NSArray arrayWithObject:@"hello@test.com"]];
            [mailCont setMessageBody:@"Your Body" isHTML:NO];
    
            [self presentViewController:mailCont animated:YES completion:nil];
        }
    
    //Delegate Method
    
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                //YOUR ACTION
                break;
            case MFMailComposeResultSent:
                //YOUR ACTION
                break;
            case MFMailComposeResultSaved:
                //YOUR ACTION
                break;
            case MFMailComposeResultFailed:
                //YOUR ACTION
                break;
            default:
                break;
        }
    }
    

    您可以通过此代码关闭视图 - [self dismissViewControllerAnimated:YES completion:nil];

    【讨论】:

      【解决方案2】:

      确保您是在真实设备而不是模拟器中测试它,并且您的设备中配置了邮件 ID

      #import <MessageUI/MessageUI.h>
      #import <MessageUI/MFMailComposeViewController.h>
      
      @interface ViewController ()<MFMailComposeViewControllerDelegate>
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      }
      
      - (IBAction)sendmail:(id)sender {
      
          if ([MFMailComposeViewController canSendMail])
          {
                  MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
                  mailer.mailComposeDelegate = self;
                  [mailer setSubject:@"Subject"];
                  NSArray *toRecipients = [NSArray arrayWithObjects:@"Recipients", nil];
                  [mailer setToRecipients:toRecipients];
                  NSString *emailBody = @"Body";
                  [mailer setMessageBody:emailBody isHTML:NO];
                  [self presentViewController:mailer animated:YES completion:nil];
          }
      }
      
      - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
      {
      
          // Close the Mail Interface
          [self dismissViewControllerAnimated:NO completion:NULL];
      }  
      

      【讨论】:

      • [self presentViewController:mailer animated:YES completion:nil];如果从自定义 uitableviewcell 内部调用它,将不起作用。怎么做?@manishsharma93
      • 在keywindow中显示控制器。将 self 替换为 [[[UIApplication sharedApplication] keyWindow] rootViewController]。
      • 如果这满足您的要求。请务必接受答案。谢谢
      • 实际上它并没有满足我的要求,因为我在从自定义 tableviewcell 类实现它的上下文中问过它。
      最近更新 更多