In this recipe we will learn about the MFMailComposeViewController
class, and how we can use this to incorporate e-mail messaging within our application.
Following on from our previous recipe, we will look at how to implement the sendEmail
method to compose an e-mail with the selected book details whenever a transfer has taken place.
To begin, follow these simple steps as outlined in the following order:
BooksViewController.h
interface file from the Project Navigator window.// BooksViewController.h // BooksLibrary // Created by Steven F Daniel on 03/12/12. // Copyright (c) 2012 GenieSoft Studios. All rights reserved. #import <UIKit/UIKit.h> #import <GameKit/GameKit.h> #import <MessageUI/MessageUI.h> #import "Books.h" @interface BooksViewController : UITableViewController<UISearchBarDelegate, GKSessionDelegate, GKPeerPickerControllerDelegate, MFMailComposeViewControllerDelegate> // Create the class instance methods -(void)sendEmail:(NSArray *)bookDetails;
BooksViewController.m
implementation file from the Project Navigator window.sendEmail:
method, as shown in the following code snippet.#pragma mark sends an email for the chosen book details - (void)sendEmail:(NSArray *)bookDetails { MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init]; mailComposer.mailComposeDelegate = self; // Check to make sure that we are set up to send mail if ([MFMailComposeViewController canSendMail]) { [mailComposer setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil]]; [mailComposer setSubject:@"Book Details"]; // Book Details for the body of our email message NSString *bookItem = [NSString stringWithFormat:@"Title:%@ Publisher: %@ Author:%@ Sort Order:%@", [bookDetails objectAtIndex:0], [bookDetails objectAtIndex:1], [bookDetails objectAtIndex:2], [bookDetails objectAtIndex:3]]; [mailComposer setMessageBody:bookItem isHTML:NO]; [mailComposer.navigationBar setTintColor:[UIColor blueColor]]; mailComposer.modalPresentationStyle = UIModalPresentationFormSheet; [self presentViewController:mailComposer animated:YES completion:nil]; } else { // Error sending the email message, so notify the user. UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Your device hasn't been set up for email" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alertMessage show]; } } #pragma mark Dismiss our Mail Controller when the user finishes - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { NSString *emailMsg = nil; // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: emailMsg = @"Email cancelled"; break; case MFMailComposeResultSent: emailMsg = @"Email sent Successfully"; break; case MFMailComposeResultFailed: emailMsg = @"Email sending failure"; break; default: emailMsg = @"Problem sending Email"; break; } // Display the alert dialog based on the message UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Book Details Email" message: emailMsg delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; // make the MFMailComposeViewController disappear [self dismissViewControllerAnimated:YES completion:nil]; }
receiveData:
method to enable e-mailing, as shown in the following code highlighted sections: -(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
// Convert our NSData type to NSString
...
...
NSError *error;
if (![managedObjectContext save:&error]) {
...
}
else {
// Send an email for the received book details
[self sendEmail:myArray];
}
...
}
In this recipe, we created a new object instance of the MFMailComposeViewController
class delegate that controls the mail dialog view, thus allowing the user to compose and send e-mail without leaving the application. We proceed to change the color of the mail composition sheet using the
navigationBar:setTintColor:
method of the controller to red, and set the subject heading and body of our e-mail message. In our next step, we set the controllers mailComposeDelegate
to itself, this ensures that our controller receives the mailComposeController:didFinishWithResult:error:
message from the MFMailComposeViewControllerDelegate
protocol when the user finishes with the e-mail dialog box. Finally, we call the controller's dismissViewControllerAnimated:
method to display the e-mail dialog box.
Deploy the application onto two different iOS devices, and then Build and Run the application by choosing Product | Run from the Product menu, or alternatively pressing Command + R.
When the compilation completes, connect each of the devices using Bluetooth, select a book from the list, and click on the Connect button. After a few seconds, a pop up is displayed, and contains a list of the nearby Bluetooth devices that the user can choose from. Once the user has selected a device, confirmation is required from that device to allow the iOS Simulator to establish a connection with it. Once a connection has been established, click on the Transfer button to transmit the book details to the other device. The following screenshot shows the application running within the iOS Simulator and the other running on an iOS device with an Internet connection and Bluetooth connectivity, as well as showing the generated e-mail for the item selected in the list.