Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 277
K.Honda Avatar asked May 19 '11 15:05

K.Honda


People also ask

Where is the Compose icon on iPhone?

Step 1: Open the Messages app on an iPhone and click the Compose button (which looks like a square with a pencil superimposed over it) found in the top right corner of the Messages home screen.

Can I compose a letter on my iPhone?

Both the iPhone and iPad can be great tools for writers, with both offering different experiences and capabilities. With the iPhone, you can write something no matter where you are, whether it's in line for coffee or just getting a quick thought down at your desk.

How do I compose an email in Apple Mail?

In the Mail app on your Mac, click the New Message button in the Mail toolbar (or use the Touch Bar). In your message, add people you want to send it to. To use other fields, such as Bcc or Priority, click the Header button , then click a field. Enter the subject of your message, then add your text.


1 Answers

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:@[@"[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:

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)
}
like image 164
Matjan Avatar answered Oct 02 '22 18:10

Matjan