Reputation: 3106
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.
Upvotes: 17
Views: 24668
Reputation: 3607
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)
}
Upvotes: 64
Reputation: 981
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
Upvotes: 13
Reputation: 1302
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:@"[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];
}
}
Upvotes: 4
Reputation: 6903
Lots of them. You can start here and here. And check stackoverflow right here.
Upvotes: 3