简体   繁体   中英

Compose mail in the iPhone app

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.

Here's the code:

Obj-C:

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

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:@[@"example@email.com"]];
        [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:

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(["example@gmail.com"])
            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)
}

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

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

Here is the code to open Mail Composer in iOS:

Source: 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];
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM