ios 目标 C:在不离开应用程序的情况下发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4862523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:44:15  来源:igfitidea点击:

Objective C: Send email without leaving app

objective-cemailios

提问by Chris

How do I send an email within an app without leaving the app.

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

This works:

这有效:

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

but goes to the mail app to send. Is there a way to do this without leaving the app?

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

回答by kubi

Yes. Use the MFMailComposeViewController.

是的。使用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:@"[email protected]"]];
    [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];
}

回答by Alexander Volkov

  1. Add MessageUI framework:

    • Click on the project
    • Select "Build Phases"
    • Expand "Link Binary With Libraries"
    • Click "+" and type "Message" to find "MessageUI" framework, then add.
  2. In current view controller add import and implement a protocol:

    #import <MessageUI/MessageUI.h> 
    #import <MessageUI/MFMailComposeViewController.h> 
    @interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
    
  1. 添加 MessageUI 框架:

    • 点击项目
    • 选择“构建阶段”
    • 展开“链接二进制与库”
    • 点击“+”并输入“Message”找到“MessageUI”框架,然后添加。
  2. 在当前视图控制器中添加导入并实现一个协议:

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

Add methods:

添加方法:

    -(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:@"[email protected]"]];
            [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];
    }

回答by AbuZubair

Updated for iOS 6. Please note that this uses ARC and does not use the deprecated modal view presentation:

为 iOS 6 更新。请注意,这使用 ARC 并且不使用已弃用的模态视图演示:

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

And then the code to present the email screen:

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

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