首页 > 解决方案 > 如何使用 MFMailComposerViewController 从自定义 UITableViewCell 发送邮件

问题描述

我想在单击自定义按钮时UITableViewCell使用MFMailComposeViewController.

感谢答案是否在 Objective-C 中。

标签: iosobjective-cmfmailcomposeviewcontroller

解决方案


确保您是在真实设备中而不是在模拟器中测试它,并且您在设备中配置了邮件 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];
}  

推荐阅读