Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send e-mail in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 625
    Comment on it

    Email application of iOS device helps in sending emails. We can compose Email using MFMailComposeViewController in iOS.
    A standard interface that manages the editing and sending of email message is provided by the MFMailComposeViewController class.

     

    Let’s take an example to implement this.

    1. Create a single view application and assign a name to it.
    2. In the Main.Storyboard drag a UIButton and create an IBAction of it.
    3. In the Project navigator select your project name and click the Build Phases option.
    4. In the Build Phases select the Link Binary With Libraries, click on the '+' sign and add MessageUI.framework.

     

    In the ViewController.m include the MFMailComposeViewControllerDelegate and import the MessageUI framework as :-

     

    #import <MessageUI/MessageUI.h>
    
    @interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
    

     

    Now in the IBAction of the UIButton include the following lines of code :-

     

    - (IBAction)btnSendMail:(id)sender {
    
        NSString *emailTitle = @"Test Email";    // Set Email title
     
        NSString *messageBody = @"iOS programming is so fun!";  // Content of the Email     
    
        MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
    
        mailCompose.mailComposeDelegate = self;   // set the delegate to self
    
        [mailCompose setSubject:emailTitle];
    
        [mailCompose setMessageBody:messageBody isHTML:NO];
        
        [self presentViewController:mailCompose animated:YES completion:NULL];   
        
    }

     

    Now implement the delegate method of the MFMailComposeViewController protocol :-

     

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{     
        
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                break;
            default:
                break;
        }
    
        [self dismissViewControllerAnimated:YES completion:NULL];
    
    }

     

    However the mail application does not work on the simulator. We need a real device for implementing this.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: