IT tutorials

Iphone Application : Using Address Book, Email, and Maps (part 4) - Using the Message UI

12/9/2012 11:36:36 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Using the Message UI

In our example of using the Message UI framework, we allow users to email a buddy by pressing the Send Mail button. We will populate the To field of the email with the address that we located in the address book. The user can then use the interface provided by the MFMailComposeViewController to edit the email and send it. As with the other features discussed this hour, we need to add the Message UI framework before a message will work.

We also need to conform to the MFMailComposeViewControllerDelegate, which includes a method mailComposeController:didFinishWithResult that is called after the user is finished sending a message.

Adding the Message UI Framework

Again, in the Xcode project window, right-click the Frameworks group and choose Add, Existing Frameworks from the menu. In the scrolling list, choose MessageUI.framework, and then click Add.

Open the BestFriendViewController.h interface file and add one more #import statement for <MessageUI/MessageUI.h>. Add MFMailComposeViewControllerDelegate to the list of protocols that we’re conforming to.

The final version of the interface is displayed in Listing 6.

Listing 6.
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <MessageUI/MessageUI.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BestFriendViewController : UIViewController
    IBOutlet UILabel *name;
    IBOutlet UILabel *email;
    IBOutlet UIImageView *photo;
    IBOutlet MKMapView *map;

@property (nonatomic, retain) UILabel *name;
@property (nonatomic, retain) UILabel *email;
@property (nonatomic, retain) UIImageView *photo;
@property (nonatomic, retain) MKMapView *map;

- (IBAction)newBFF:(id)sender;
- (IBAction)sendEmail:(id)sender;



Displaying the Message Composer

To compose a message, we need to allocate an initialize an instance of MFMailComposeViewController. This modal view is then added to our view with presentModalViewController:animated. To set the To recipients, we use the appropriately named MFMailComposeViewController method setToRecipients. One item of interest is that the method expects an array, so we need to take the email address for our buddy and create an array with a single element in it so that we can use the method.

Speaking of the email address, where we will access it? Simple! Earlier we set the email UILabel to the address, so we’ll just use email.text to get the address of our buddy.

Create the sendEmail method using Listing 7 as your guide.

Listing 7.
 1: - (IBAction)sendEmail:(id)sender {
 2:     MFMailComposeViewController *mailComposer;
 3:     NSArray *emailAddresses;
 4:     emailAddresses=[[NSArray alloc]initWithObjects: email.text,nil];
 6:     mailComposer=[[MFMailComposeViewController alloc] init];
 7:     mailComposer.mailComposeDelegate=self;
 8:     [mailComposer setToRecipients:emailAddresses];
 9:     [self presentModalViewController:mailComposer animated:YES];
11:     [emailAddresses release];
12:     [mailComposer release];
13: }

Unlike some of the other methods we’ve written this hour, there are few surprises here. Line 2 declares mailComposer as an instance of MFMailComposeViewController—the object that displays and handles message composition. Lines 3–4 define an array, emailAddresses, that contains a single element grabbed from the email UILabel.

Lines 6–8 allocate and initialize the MFMailComposeViewController object, setting its delegate to self (BestFriendViewController) and the recipient list to the emailAddresses array. Line 9 presents the message composition window onscreen.

Finally, lines 11–12 release the array of email addresses and the mailComposer MFMailComposeViewController object.

Handling Message Completion

When a user is finished composing/sending a message, the modal composition window should be dismissed and the MFMailComposeViewController object released. To do this, we need to implement the mailComposeController:didFinishWithResult method defined in the MFMailComposeViewControllerDelegate protocol.

Add this final method to the BestFriendViewController.m file:

- (void)mailComposeController:(MFMailComposeViewController*)controller
                        error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];

All that is needed is the single line to dismiss the modal view, and with that, we’re done!

Use Build and Run to test the application. Select a contact and watch as the map finds your friend’s home location, zooms in, then sets an annotation. Use the Email button to compose and send an email. Fun and excitement for all!

In this project, shown in Figure 3, we’ve combined mapping, email, and address book features in a single, integrated application. You should now have some ideas about what is possible when you integrate existing iPhone application features into your software.

Figure 3. Mapping, email, and address book integration—all in one app.
- Iphone Application : Using Address Book, Email, and Maps (part 3) - Using a Map Object
- Iphone Application : Using Address Book, Email, and Maps (part 2) - Accessing the Address Book
- Iphone Application : Using Address Book, Email, and Maps (part 1)
- Iphone Application : Extending Application Integration
- Introducing Windows Phone 8 : Phone Specifications
- Introducing Windows Phone 8 : A Different Kind of Phone
- iPhone Application Development : Working with Rich Media - Accessing and Playing the iPod Library
- Android Application Development : Initialization in (part 2)
- Android Application Development : Initialization in (part 1)
- Android Application Development : Initialization Parameters in AndroidManifest.xml
Top 10
Technology FAQ
- Microsoft ebs security server configuration
- IIs7 on Windows server 2003
- How to Configure Failover Clusters With Win 2008 Server R2?
- Windows 2008 Network Load Balancing
- Windows Server 2008 - Group Policy Management - Remove Computer Management
- Remove shortcuts possibility in a web page or to put in favorite
- HTA Dynamic Drop Down List
- IIS host header and DNS
- VMware or MS Virtual Server?
- Adobe Acrobat 9 inserting tab pages
programming4us programming4us