iPhone Coding Snippet – In Application Emailing

    November 18th, 2009 Posted by: - posted under:Snippets

    IMG_0001A lot of applications you see have an email button. When you click it then it will leave the application and take you to the Mail application. It can get really annoying leaving the application and then going back in after your done sending the email. This is just a great way to show off your app and make your app look more professional and make it easier on the user by filling in the To and Subject for them. They are also able to change the From to whatever email then want you to receive an email from just like you can do in the Mail app. So todays tutorial is gonna show you how to email within your application. We will be using the MessageUI framework and we will also include an attachment in the email. Theres a screenshot to the left of how it will look.

    So lets get started…

    1. Create A New View Based Application
    You can name yours whatever you want, in the tutorial I will be referring it as the NameViewControllers.

    2. Import The Frameworks

    The first thing we need to do is import the framework. So go to your Frameworks folder in the Files In Pain section on the left. Open the folder and right click one of the frameworks and click “Reveal In Finder.” Heres a screenshot on what you should do.

    Screen shot 2009-11-18 at 7.58.04 PM

    Go ahead and look for the “MessageUI.framework” and highlight it then drag it into your frameworks folder. Make sure that when you click Add on the thing that makes sure you want to import it that “Copy items into destination group’s folder (if needed)” is not check. Thats a very important step or you will have big time problems and you do this with any frameworks you would ever use in the future.

    3. Implementing The Code

    Now that we have imported the framework lets get into some coding. So go ahead and jump in the NameViewController.H and make an IBAction for a button. Then copy that code and paste it in the NameViewController.M with curly brackets. Also make sure to add the button in Interface Builder and link it up with a Touch Up Inside method. You guys are at the point to knowing how to hook up actions and dragging stuff into Interface Builder. After that we want to on the top of the NameViewController.h import the framework. So on the top do #import “MessageUI/MessageUI.h” and the reason why we do this is because we must import out MessageUI framework to make calls to the associated header files. Now we need to also put in the delegate protocols for this framework. @interface NameViewController: UIViewController do this code <MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>. Heres the code here for the NameViewController.H

    #import <MessageUI/MessageUI.h>

    @interface MailComposerViewController : UIViewController

    <MFMailComposeViewControllerDelegate,UINavigationControllerDelegate> {

    }

    -(IBAction)pushEmail;

    @end


    Now after your done with the .H jump into the .M viewcontroller. Now in your button action code do this:

    -(IBAction)pushEmail {

    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];

    mail.mailComposeDelegate = self;

    if ([MFMailComposeViewController canSendMail]) {

    //Setting up the Subject, recipients, and message body.

    [mail setToRecipients:[NSArray arrayWithObjects:@”email@email.com”,nil]];

    [mail setSubject:@”Subject of Email”];

    [mail setMessageBody:@”Message of email” isHTML:NO];

    //Present the mail view controller

    [self presentModalViewController:mail animated:YES];

    }

    //release the mail

    [mail release];

    }

    //This is one of the delegate methods that handles success or failure

    //and dismisses the mail

    – (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

    {

    [self dismissModalViewControllerAnimated:YES];

    if (result == MFMailComposeResultFailed) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Message Failed!” message:@”Your email has failed to send” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:nil];

    [alert show];

    [alert release];

    }

    }


    What if we wanted to include an image attachment to the email? Well its quite simple. Just add this to the code right under the part where you set up the Recipient, Subject, and the Message.

    UIImage *pic = [UIImage imageNamed:@”Funny.png”];

    NSData *exportData = UIImageJPEGRepresentation(pic ,1.0);

    [mail addAttachmentData:exportData mimeType:@”image/jpeg” fileName:@”Picture.jpeg”];

    Now we are setting up the MailComposer in the first part of the code in the action. Then we call a didFinishWithResult method where we are setting up if the email fails or sends. Also it sets up a Cancel button for you so we have to call the dismiss method so that it works. In the attachment code just edit the imageNamed:@”” with your images name.That is basically it! The source code is below for the people that just doesn’t wanna copy and paste or type… I am just jking with you guys. Happy iCoding!

    iPhone Tutorial – In Application Emailing