Using Apple’s Mail Composer
Description:
iOS 3.0 and above includes a great built in mail composer. This mail composer is called MFMailComposeViewController. This composer view controller looks great and is native and is the same thing you would get in the mail app.
Tutorial Info:
I will be walking through step by step on how to create a project that will first check and see if the user’s device has mail setup. If the user’s device is set up for mail then the composer will show and on send it will send the email using the default email set for the device.
Step 1:
We will first start with a “View Based Application” project. Since we are using the MFMailComposeViewController, we will need to add the MessageUI framework. To do this right click on the “Frameworks” Group
Step 2:
Now our initial project is all set up. We should first open up the view controller we will be in when we want to present the mail composer. Open the header file (.h) and import the MessageUI framework which will allow us to use the composer.
#import <MessageUI/MessageUI.h>
Next we will need to make this class a MFMailComposeViewControllerDelegate this view controller can become the delegate.
@interface SendMailViewController : UIViewController<MFMailComposeViewControllerDelegate> { }
Step 3:
We now need a method to launch the composer. We call this method sendMail. We will connect this to a button in interface builder so make the method an IBAction.
- (IBAction)sendMail;
Now hop into the implementation file (.m) and lets set up this method
- (IBAction)sendMail { }
Step 4:
Also, lets implement the delegate method for MFMailComposeViewController.
#pragma mark - #pragma mark MFMailComposeViewControllerDelegate Methods - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { }
Step 5:
Then go back to the sendMail function and we need to see if the user’s device is set up to send mail. The MFMailComposeViewController class has a canSendMail function that returns a boolean whether or not mail is set up on the device. Lets add that now.
if ([MFMailComposeViewController canSendMail]) { }else { }
Now that we have this check, if mail is not set up we can show a UIAlertView notifying the user that mail is not yet set up on the device.
if ([MFMailComposeViewController canSendMail]) { }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"Your phone is not currently configured to send mail." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; }
Now let’s handle the other option (if mail is set up). We need to first create a MFMailComposeViewController and assign the delegate to this view controller (self) so it can call the delegate method we added. Then we need to present it and release it.
if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mfViewController = [[MFMailComposeViewController alloc] init]; mfViewController.mailComposeDelegate = self; [self presentModalViewController:mfViewController animated:YES]; [mfViewController release]; }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"Your phone is not currently configured to send mail." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; }
Step 6:
Now we should take care of the delegate method so we know what the status of the sent message is and so we can dismiss the composer view controller.
In this method we will create an UIAlertView that will present the user the status. Then we will take the given result from the composer and assign the alert with the correct status. Next, we will dismiss the composer view controller. Last we will show the alert and release it.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; switch (result) { case MFMailComposeResultCancelled: alert.message = @"Message Canceled"; break; case MFMailComposeResultSaved: alert.message = @"Message Saved"; break; case MFMailComposeResultSent: alert.message = @"Message Sent"; break; case MFMailComposeResultFailed: alert.message = @"Message Failed"; break; default: alert.message = @"Message Not Sent"; break; } [self dismissModalViewControllerAnimated:YES]; [alert show]; [alert release]; }
Step 7:
Now everything is set we need to make a button in interface builder and connect it to the sendMail method.
That is it, the app is ready to send mail through the composer!
That is it, the app is ready to send mail through the composer!
No comments:
Post a Comment