Saturday, October 27, 2012

Sending E-mail from within Your App with the Message UI Framework

While it has always been possible to send e-mail from within an iPhone application using a mailto: URL, doing so has been a jarring experience for the user, as using a mailto: URL kicks the user out of the currently running application and launches the Mail application. This, needless to say, does not result in an ideal user experience. Furthermore, the amount of customizability in terms of not just composing the e-mail itself, but also in terms of being able to take action after the e-mail has been sent, has been very limiting.

Because of this, with iPhone OS 3, Apple has introduced the Message UI framework, which enables you to present to the user a fully customizable interface inside your application for the user to compose and send an e-mail. It provides a class called MF Mail Compose View Controller and the associated delegate protocol for MF Mail Compose View Controller, MF Mail Compose View Controller Delegate.

The MF Mail Compose View Controller is a view controller just like any other, and can be presented to the user either through a navigation controller or modally using the method present Modal View Controller :animated:. Before attempting to display an MF Mail Compose View Controller, however, you should first check to see if the device has been configured to send mail. You do this by calling the class method can Send Mail on the MF Mail Compose View Controller class. If this method returns NO, then you should not attempt to send mail. If, however, this method returns YES, then you can proceed.  The MF Mail Compose View Controller has several methods on it that enable you to set various attributes of the e-mail that will be sent. They are listed in Table.

These methods should be called before the view is displayed to the user. Do not call these methods after displaying the view. So, for example, to display an e-mail composition view to send an e-mail with attached images to someone, you might do something like Listing. Essentially, first you check to see if this device can send e-mail at all. If it can’t, then you display an error to the user explaining the situation. If it can, then you create a new MF Mail Compose View Controller, and set the attributes of the e-mail that you would like to send. In this case, you are attaching an image, so you need to convert the passed-in UI Image View object to a representation that can be sent via e-mail — in this case, a PNG. You then attach it to the e-mail using a mime type of image/PNG. After that, you set you as the mail compose delegate and then present the controller modally as a child of whatever your parent controller is. Because the controller has now been added to the hierarchy, you can release it here.

BIO:
Brents Lyons is an IT professional from Exam key. Are you really looking for this 640-822 exam assistance? Move ahead to take the benefit of 640-802 and pass your exam easily .


No comments:

Post a Comment