首先在您的项目中添加 MessageUI 框架并将这两个导入您的文件中
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
然后制作一个按钮以在 xid 中引用邮件并将其与 IBAction 链接(如下所示):
-(IBAction)emailSettings:(id)sender{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"EMAIL" message:@"No Settings For Email" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"StampedeBreakfast!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"third@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSString *emailBody = [NSString stringWithFormat:@"Hey! This is my Email Body"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog( @"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog( @"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
希望对你有帮助!!