目标C:不用离开应用就发送电子邮件

如何在不离开应用的情况下在应用内发送电子邮件。

这工作:

-(void) sendEmailTo:(NSString *)to withSubject:(NSString *)subject withBody:(NSString *)body { NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@", [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]]; } 

但去邮件应用程序发送。 有没有办法做到这一点,而不离开应用程序?

是。 使用MFMailComposeViewController 。

 // From within your active view controller if([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; mailCont.mailComposeDelegate = self; [mailCont setSubject:@"yo!"]; [mailCont setToRecipients:[NSArray arrayWithObject:@"joel@stackoverflow.com"]]; [mailCont setMessageBody:@"Don't ever want to give you up" isHTML:NO]; [self presentModalViewController:mailCont animated:YES]; [mailCont release]; } // Then implement the delegate method - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self dismissModalViewControllerAnimated:YES]; } 
  1. 添加MessageUI框架:

    • 点击该项目
    • select“build立阶段”
    • 展开“与库链接二进制文件”
    • 点击“+”,然后input“Message”find“MessageUI”框架,然后添加。
  2. 在当前的视图控制器中添加导入并实现一个协议:

     #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> @interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate> 

添加方法:

  -(void)sendEmail { // From within your active view controller if([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send [mailCont setSubject:@"Email subject"]; [mailCont setToRecipients:[NSArray arrayWithObject:@"myFriends@email.com"]]; [mailCont setMessageBody:@"Email message" isHTML:NO]; [self presentViewController:mailCont animated:YES completion:nil]; } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [controller dismissViewControllerAnimated:YES completion:nil]; } 

针对iOS 6更新。请注意,这使用ARC,不使用已弃用的模式视图表示:

 #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> @interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate> 

然后显示电子邮件屏幕的代码:

 - (IBAction)emailButtonPushed:(id)sender { if([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; mailCont.mailComposeDelegate = self; [mailCont setSubject:@"Your email"]; [mailCont setMessageBody:[@"Your body for this message is " stringByAppendingString:@" this is awesome"] isHTML:NO]; [self presentViewController:mailCont animated:YES completion:nil]; } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { //handle any error [controller dismissViewControllerAnimated:YES completion:nil]; }