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

Chr*_*ris 39 email objective-c 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]];
}
Run Code Online (Sandbox Code Playgroud)

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

kub*_*ubi 54

是.使用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];
}
Run Code Online (Sandbox Code Playgroud)

  • 别忘了:#import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> (5认同)

Ale*_*kov 27

  1. 添加MessageUI框架:

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

    #import <MessageUI/MessageUI.h> 
    #import <MessageUI/MFMailComposeViewController.h> 
    @interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
    
    Run Code Online (Sandbox Code Playgroud)

添加方法:

    -(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];
    }
Run Code Online (Sandbox Code Playgroud)


Abu*_*air 12

针对iOS 6进行了更新.请注意,这使用ARC并且不使用已弃用的模式视图演示:

#import <MessageUI/MessageUI.h> 
#import <MessageUI/MFMailComposeViewController.h> 
@interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
Run Code Online (Sandbox Code Playgroud)

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)