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

时间:2011-02-01 12:23:51

标签: objective-c email ios

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

这有效:

-(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]];
}

但转到邮件应用程序发送。有没有办法在不离开应用程序的情况下执行此操作?

3 个答案:

答案 0 :(得分:53)

是。使用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 presentViewController:mailCont animated:YES completion:nil];

}


// Then implement the delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissViewControllerAnimated:YES completion:nil];
}

答案 1 :(得分:27)

  1. 添加MessageUI框架:

    • 点击项目
    • 选择“构建阶段”
    • 展开“使用库链接二进制文件”
    • 单击“+”并键入“Message”以查找“MessageUI”框架,然后添加。
  2. 在当前视图控制器中添加导入并实现协议:

    #import <MessageUI/MessageUI.h> 
    #import <MessageUI/MFMailComposeViewController.h> 
    @interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
    
  3. 添加方法:

        -(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];
        }
    

答案 2 :(得分:12)

针对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];
}