【问题标题】:Objective C: Send email without leaving app目标 C:在不离开应用程序的情况下发送电子邮件
【发布时间】:2011-06-19 06:01:15
【问题描述】:

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

这行得通:

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

但会转到邮件应用程序发送。有没有办法在不离开应用的情况下做到这一点?

【问题讨论】:

标签: objective-c email ios


【解决方案1】:

是的。使用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];
}

【讨论】:

  • 这种方法有效,但完成后不会精确到邮件屏幕
  • 你还需要实现委托方法,我加的。
  • 行 mailCont.delegate = self;应该阅读:mailCont.mailComposeDelegate = self; delegate 是 UINavigationController 的,MFMailComposeViewController 是它的子类。
  • 别忘了:#import #import
  • "[self presentModalViewController:mailCont animated:YES];"被贬低了。使用,“[self presentViewController:mailCont animated:YES completion:nil];”和“[self dismissViewControllerAnimated:YES];”也已弃用。使用,“[self dismissViewControllerAnimated:YES completion:nil];”
【解决方案2】:
  1. 添加 MessageUI 框架:

    • 点击项目
    • 选择“构建阶段”
    • 扩展“将二进制文件与库链接”
    • 点击“+”,输入“Message”,找到“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];
    }

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 2014-10-10
      • 2014-01-01
      • 1970-01-01
      • 2018-07-04
      • 2021-12-15
      相关资源
      最近更新 更多