ios 在 iPhone 应用程序中撰写邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6061039/
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 20:00:41  来源:igfitidea点击:

Compose mail in the iPhone app

iphoneiosemailios4

提问by K.Honda

In my app, I have an abouts page. I would like to place a round rect button which when I press it, I would be able to send an email to the company with an embedded email address.

在我的应用程序中,我有一个关于页面。我想放置一个圆形矩形按钮,当我按下它时,我可以向公司发送一封带有嵌入式电子邮件地址的电子邮件。

Are there any tutorials to do this?

是否有任何教程可以做到这一点?

Thanks in advance.

提前致谢。

采纳答案by martiall

You have to link against the MessageUIframework and use the class MFMailComposeViewController. Don't forget to import the framework (#import <MessageUI/MessageUI.h>).

您必须链接到MessageUI框架并使用类MFMailComposeViewController。不要忘记导入框架 ( #import <MessageUI/MessageUI.h>)。

The documentation with sample code: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

带有示例代码的文档:http: //developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

回答by Matjan

Here's the code:

这是代码:

Obj-C:

对象-C:

(Don't forget to add the messageUI framework to your project!!!)

(不要忘记将 messageUI 框架添加到您的项目中!!!)

First import the message library:

首先导入消息库:

#import <MessageUI/MessageUI.h>

Then mark your self as a delegate like this:

然后像这样将自己标记为委托:

@interface MYViewController () <MFMailComposeViewControllerDelegate>

Then to pull up the composer (if user has email set up on their device):

然后拉起作曲家(如果用户在他们的设备上设置了电子邮件):

- (IBAction)emailButtonPressed:(id)sender
{
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"[email protected]"]];
        [composeViewController setSubject:@"example subject"];
        [self presentViewController:composeViewController animated:YES completion:nil];
    }
}

Then to handle the delegate callback and dismiss the composer:

然后处理委托回调并关闭作曲家:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    //Add an alert in case of failure
    [self dismissViewControllerAnimated:YES completion:nil];
}

SWIFT 3:

快速 3:

Import the relevant library:

导入相关库:

import MessageUI

Mark your view controller as a delegate like so:

将您的视图控制器标记为委托,如下所示:

class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {

Pull up composer (if user has email set up on their device):

拉起作曲家(如果用户在他们的设备上设置了电子邮件):

@IBAction func emailButtonAction(_ sender: UIButton) {

        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["[email protected]"])
            mail.setSubject("Example Subject")
            mail.setMessageBody("<p>Test</p>", isHTML: true)
            present(mail, animated: true)
        }
    }

Handle delegate callback and dismiss the composer:

处理委托回调并关闭作曲家:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
}

回答by Santosh

Here is the code to open Mail Composer in iOS:

这是在 iOS 中打开 Mail Composer 的代码:

Source:http://sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html

来源:http: //sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html

#import "ViewController.h"
@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Send Mail" forState:UIControlStateNormal];
    button.frame = CGRectMake(100.0, 350.0, 100.0, 40.0);
    [self.view addSubview:button];

}
- (void) send:(id)sender{
    MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];
    [comp setMailComposeDelegate:self];
    if([MFMailComposeViewController canSendMail])
    {
        [comp setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
        [comp setSubject:@"From my app"];
        [comp setMessageBody:@"Hello bro" isHTML:NO];
        [comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:comp animated:YES completion:nil];
    }
    else{
        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alrt show];

    }
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if(error)
    {
        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alrt show];
        [self dismissModalViewControllerAnimated:YES];
    }
    else{
        [self dismissModalViewControllerAnimated:YES];
    }

}

回答by Todd Hopkinson

Lots of them. You can start hereand here. And check stackoverflow right here.

其中很多。你可以从这里这里开始。并在此处检查 stackoverflow 。