在MessageUI中设置发件人的步骤如下:
@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate>
MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
mailComposeVC.mailComposeDelegate = self;
[mailComposeVC setToRecipients:@[@"sender@example.com"]];
[self presentViewController:mailComposeVC animated:YES completion:nil];
完整的代码示例:
#import <MessageUI/MessageUI.h>
@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate>
// ...
@end
@implementation YourViewController
// ...
- (void)sendEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
mailComposeVC.mailComposeDelegate = self;
[mailComposeVC setToRecipients:@[@"sender@example.com"]];
[self presentViewController:mailComposeVC animated:YES completion:nil];
} else {
// 当设备不支持发送邮件时的处理逻辑
}
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
// 处理邮件发送结果和错误
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
这样,你就可以使用Objective-C在MessageUI中设置发件人了。注意,为了使邮件发送成功,需要在真实的设备上进行测试,并且确保设备已经配置了有效的邮件账户。
领取专属 10元无门槛券
手把手带您无忧上云