iPhone应用程序中如何撰写邮件

17

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

有没有教程可以做到这一点?

提前致谢。

4个回答

64

以下是代码:

Obj-C:

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

首先导入message库:

#import <MessageUI/MessageUI.h>

然后像这样将自己标记为代表:

@interface MYViewController () <MFMailComposeViewControllerDelegate>

然后拉起邮件编辑器(如果用户在其设备上设置了电子邮件):

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

然后处理委托回调并关闭创作器:

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

SWIFT 3:

导入相关库:

import MessageUI

像这样将您的视图控制器标记为代理:

class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {

打开Composer(如果用户在其设备上设置了电子邮件):

@IBAction func emailButtonAction(_ sender: UIButton) {
        
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["example@gmail.com"])
            mail.setSubject("Example Subject")
            mail.setMessageBody("<p>Test</p>", isHTML: true)
            present(mail, animated: true)
        }
    }

处理委托回调并关闭撰写器:

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

在iOS 6中创建composerVC时,我似乎遇到了崩溃问题*** Assertion failure in NSDictionary *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit_Sim/UIKit-2380.17/UIAppearance.m:1118。我使用这个答案解决了它。 - Fonix
那个问题与UIAppearance有关,上述仍然是实现发送电子邮件的正确方式。 - Matjan

13

4

这是在iOS中打开邮件撰写器的代码:

源代码: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:@"imstkgp@gnail.com", 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];
    }

}

我不确定我们是否应该使用self [dismissModalViewControllerAnimated:YES];代替,我认为我们应该使用[controller dismissModalViewControllerAnimated:YES];。 - Matrosov Oleksandr

3
很多。您可以从这里这里开始学习。并且在这里查看stackoverflow的相关问题:这里

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接