Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

iOS sends e-mail


May 21, 2021 iOS Development Manual


Table of contents


IOS sends an e-mail message


Brief introduction

We can use the e-mail application in the IOS device to send e-mail messages.

The instance step

1, create a simple View base application

2, select the project file, then select the target, and then add MessageUI.framework

3. Add a button to ViewController.xib to create an action for sending e-mail (action)

4, update ViewController.h, as shown below

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
{
    MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

@end

5, as shown below, update viewController .m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)sendMail:(id)sender{
    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Test mail"];
    [mailComposer setMessageBody:@"Testing message 
    for the test mail" isHTML:NO];
    [self presentModalViewController:mailComposer animated:YES];
}

#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller 
 didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   if (result) {
        NSLog(@"Result : %d",result);
    }
    if (error) {
        NSLog(@"Error : %@",error);
    }
    [self dismissModalViewControllerAnimated:YES];

}

@end

Output

When you run the application, you will see the output below

iOS sends e-mail

When you click the send email button, you can see the following results:

iOS sends e-mail