iPhone Tutorial - In app Email

Discussion in 'iOS Development' started by Collateral, Nov 10, 2009.

  1. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Ok, so here is a tutorial for getting the Mail.app modal view in your application for OS 3.0+ and to launch Mail.app from your application if your on 2.X.X.

    99% of the time, this is going to be run as an event, or a response to user input (pressing a UIButton, responding to a UIAlertView, etc.), so right off the bat, you know its going to be a -(IBAction) (if, of course, your using the Interface builder).
    First, however, we are going to want to import the mail framework, titled "MessageUI.framework". So right click on the "Frameworks" folder, then click "Add" then "Existing Frameworks" then "MessageUI.framework". Now you should see this:

    Please Register or Log in to view images



    Then you are going to want to add this to your import statements:
    [OBJC]#import <MessageUI/MFMailComposeViewController.h>[/OBJC]​


    This will allow you to access those oh-so-special frameworks you just added. We now just have to add the methods to call the Mail.app modal view. So add these to your .h file under your @property calls.
    [OBJC]
    - (void)displayComposerSheet;
    - (void)showPicker;
    - (void)launchMailAppOnDevice;
    - (IBAction)callMail;[/OBJC]

    This is the call for the methods that we are going to define in your .m file. Speaking of which, turn to your .m file now, and I will break down what is necessary to call to make this puppy work.
    First, we need an -(IBAction) to call our method. This is what callMail { } is for. So make a UIButton and assign callMail { } to it. If don't know how to do this, then the easiest way in in the Interface Builder. In the IB, click on "File's Owner" and then on the "Connections" tab in the inspector. You should see "Received Actions" and under that, "callMail". Click on the "+" sign next to callMail, and drag it to your UIButton. It will give you a drop-down menu. Choose "Touch Up Inside." As shown:

    Please Register or Log in to view images


    Save your .XIB and go back to your .m file. Type:

    [OBJC]- (IBAction) callMail {
    [self showPicker];
    }[/OBJC]​

    Ok, now your - (IBAction) is done. Wasn't that easy? Now here comes the method definitions. Just type them in exactly as i write them:

    [OBJC]-(void)showPicker
    {
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
    // We must always check whether the current device is configured for sending emails
    if ([mailClass canSendMail])
    {
    [self displayComposerSheet];
    }
    else
    {
    [self launchMailAppOnDevice];
    }
    }
    else
    {
    [self launchMailAppOnDevice];
    }
    }
    [/OBJC]​

    This is the method we just called. This method checks if the device can bring up the modal view. If it can, it calls the method to do so. If it can't, it tells the divice to open Mail.app. Now we need to call the modal view itself:

    [OBJC]// Displays an email composition interface inside the application. Populates all the Mail fields.
    -(void)displayComposerSheet
    {
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject

    Please Register or Log in to view images

    "SUBJECT HERE"];


    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject

    Please Register or Log in to view images

    "EMAIL HERE"];
    //NSArray *ccRecipients = [NSArray arrayWithObjects

    Please Register or Log in to view images

    "", nil];
    //NSArray *bccRecipients = [NSArray arrayWithObject

    Please Register or Log in to view images

    ""];

    [picker setToRecipients:toRecipients];
    //[picker setCcRecipients:ccRecipients];
    //[picker setBccRecipients:bccRecipients];

    // Fill out the email body text
    NSString *emailBody = (@"BODY TEXT HERE");
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController

    Please Register or Log in to view images

    icker animated:YES];
    [picker release];
    }
    [/OBJC]​

    The above will call the mail composition GUI as a modal view inside your application. From here, you can set the subject, recipients, cc's, bcc's and the body text. (This, of course, ends up being totally editable by the end user, however).


    [OBJC]// Dismisses the email composition interface when users tap Cancel or Send.
    - (void)mailComposeController

    Please Register or Log in to view images

    MFMailComposeViewController*)controller didFinishWithResult

    Please Register or Log in to view images

    MFMailComposeResult)result error

    Please Register or Log in to view images

    NSError*)error
    {
    [self dismissModalViewControllerAnimated:YES];
    }[/OBJC]​

    This, as the comment says, dismissed the composition view when the user sends or cancels the email.
    And finally we must type the method to allow the 2.X.X users to call their Mail.app.

    [OBJC]// Launches the Mail application on the device.
    -(void)launchMailAppOnDevice
    {
    NSString *recipients = @"RECIPIENTS HERE";
    NSString *body = (@"BODY HERE");

    NSString *email = [NSString stringWithFormat

    Please Register or Log in to view images

    "%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
    }
    [/OBJC]​

    And there you have it. When you click your UIButton, In-app mail should now work

    Please Register or Log in to view images




    http://collateraldamag3.blogspot.com/2009/11/in-app-email.html
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You need not use the NSClassFromString function; [MFMailComposerViewController canSendMail] will suffice.

    If you'd like to make this a consummate tutorial, you should include the delegate methods which allow the program to react to various user actions such as canceling the operation.

    P.S. not everyone uses IB, so we dont "know it's going to be -(IBAction) right off the bat".

    Please Register or Log in to view images

    There's my nitpicking for today.
  3. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    ....thanks.

    selfEsteem = (selfEsteem - 1);
  4. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    IBAction is always void.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Remove your images of code and replace them with actual code. Wrap the code in [OBJC] tags. It will make thigns much easier for those with older monitors, or whose broswers resize the images so they are far too compressed to read. It also helps if people who look at the tutorial can actually have the code to copy into their apps so they can see it in action.


    I didn't even realize that sending mail was an issue for anyone. It's pretty straight forward. If I knew there were issues/concerns, I would have gone more into depth here (where I use mail because it's the simplest way to show an image).

    Nolesfar, more specifically, IBAction is a typedef'd void.
  6. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    I dunno if anyone has trouble with it, but the best way to learn is to teach, so i decided to put up tuts on stuff

    Please Register or Log in to view images



    haha

    And about the OBJC tags - Will do.

Share This Page