Chapter 16. Accessing Built-in Applications

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to send emails from within your application

  • How to invoke Safari from within your application

  • How to invoke the Phone from within your application

  • How to send SMS messages from within your application

  • How to access the camera and Photo Library

  • How to access the Contacts application

  • How to add and remove contact information from the Contacts application

The iPhone comes with a number of built-in applications that make it one of the most popular mobile devices of all time. Some of these applications are Contacts, Mail, Phone, Safari, SMS, and Calendar. These applications perform most of the tasks you would expect of a mobile phone. As an iPhone developer, you can also programmatically invoke these applications from within your application using the various APIs provided by the iPhone SDK.

In this chapter, you learn how to invoke some of the built-in applications that come bundled with iPhone as well as how to interact with them from within your iPhone application.

SENDING EMAILS

Sending emails is one of the many tasks performed by iPhone users. Sending emails on the iPhone is accomplished using the built-in Mail application, which is a rich HTML mail client that supports POP3, IMAP, Exchange email systems, and most web-based emails such as Yahoo! and Gmail.

There are times where you need to send an email in your iPhone application. A good example is embedding a feedback button in your application that users can click to send feedback to you directly. You have two ways to send emails programmatically:

  • Build your own email client and implement all the necessary protocols necessary to communicate with an email server

  • Invoke the built-in Mail application and ask it to send the email for you

Unless you are well versed in network communications and familiar with all the email protocols, your most logical choice is to go for option two — invoke the Mail application to do the job. The following Try It Out shows you how (you need to download the code files indicated here to work through this example).

Invoking Safari

If you want to invoke the Safari Web browser on your iPhone, you can also make use of a URL string and then use the openURL: method of the application instance, like this:

[[UIApplication sharedApplication]
openURL:[NSURL URLWithString: @"http://www.apple
.com"]];

The preceding code snippet invokes Safari to open the www.apple.com page (see Figure 16-3).

Figure 16-3

Figure 16.3. Figure 16-3

Invoking the Phone

To make a phone call using the iPhone's dialer, use the following URL string:

[[UIApplication sharedApplication]
        openURL:[NSURL URLWithString:@"tel:1234567890"]];

The preceding statement invokes the dialer of the iPhone using the phone number specified.

Note

Note that the preceding statement works only for iPhone and not iPod Touch because the iPod Touch does not have phone capabilities. Also, you would need to a real device to test this out; the code does not have an effect on the iPhone Simulator. Appendix E discusses how to prepare your iPhone for testing.

Invoking SMS

You can also use a URL string to send SMS messages using the SMS application:

[[UIApplication sharedApplication]
            openURL:[NSURL URLWithString:
            @"sms:96924065"]];

The preceding statement invokes the SMS application (see Figure 16-4).

Figure 16-4

Figure 16.4. Figure 16-4

Note

Note that the preceding statement works only for iPhone and not iPod Touch, because the iPod Touch does not have phone capabilities. Also, you would need to a real device to test this out; the code does not have an effect on the iPhone Simulator. Appendix E discusses how to prepare your iPhone for testing.

ACCESSING THE CAMERA AND THE PHOTO LIBRARY

The iPhone and iPod Touch both have cameras that allow you to take pictures as well as record videos. All the pictures taken and videos recorded are saved in the Photos application. As a developer, you have a number of options to manipulate the cameras as well as access the pictures and videos stored in the Photos application:

  • You can invoke the Camera to take pictures or record a video.

  • You can invoke the Photos application to allow users to select a picture or video from the photo albums. You can then use the picture or video selected in your application.

Accessing the Photo Library

Every iPhone and iPod Touch device includes the Photos application, in which all the pictures taken and videos recorded are stored. Using the iPhone SDK, you can use the UIImagePickerController class to programmatically display a UI to let the user select your pictures and videos from the Photos application. The following Try It Out demonstrates how you can do that in your application.

Accessing the Camera

Besides accessing the Photo Library, you can also access the camera on your iPhone. Although accessing the hardware is the focus of the next chapter, you take a look here at how to access the camera in this chapter because it is also accomplished using the UIImagePickerController class.

To access the camera, you modify the existing project created in the previous section. There isn't much to modify because most of the code you have written still applies.

ACCESSING THE CONTACTS APPLICATION

Another commonly used built-in application in your iPhone and iPod Touch is the Contacts application (see Figure 16-7). The Contacts application contains the list of contacts you have saved on your device.

Figure 16-7

Figure 16.7. Figure 16-7

As with the Photo Library, you can programmatically access the contacts stored in the Contacts application. This access is useful because your application can rely on the Contacts application as a backend storage area for contact information, rather than need to create your own database to do so.

In the next Try It Out, you see how to access the Contacts application using the ABPeoplePickerNavigationController class from the AddressBookUI framework. Don't forget to download the code files indicated here.

Adding a Contact

Besides retrieving the information of contacts stored in the Contacts application, you might also want to add a new contact directly into it. You can do so via the code snippets below:

-(void) addContact{

    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABPersonCreate();

    //---add the first name and last name---
    ABRecordSetValue(person, kABPersonFirstNameProperty, @"Wei-Meng", nil);
    ABRecordSetValue(person, kABPersonLastNameProperty, @"Lee", nil);

    //---add the address---
    ABMutableMultiValueRef address =
        ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    [addressDictionary setObject:@"Some Street Name" forKey:(NSString *)
        kABPersonAddressStreetKey];
    [addressDictionary setObject:@"New York" forKey:(NSString *)
        kABPersonAddressCityKey];
    [addressDictionary setObject:@"NY" forKey:(NSString *)
        kABPersonAddressStateKey];
    [addressDictionary setObject:@"12345" forKey:(NSString *)
        kABPersonAddressZIPKey];
    [addressDictionary setObject:@"United States" forKey:(NSString *)
        kABPersonAddressCountryKey];
    [addressDictionary setObject:@"US" forKey:(NSString *)
        kABPersonAddressCountryCodeKey];

    ABMultiValueAddValueAndLabel(address, addressDictionary, kABHomeLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, nil);

    //---add the address book for the contact and save the addressbook---
    ABAddressBookAddRecord(addressBook, person, nil);
    ABAddressBookSave(addressBook, nil);
    CFRelease(person);
}

The above method-addContact, adds a contact to the Contacts application. It populates the contact with the following information:

  • First Name

  • Last Name

  • Home address

    • Street

    • City

    • State

    • Zip

    • Country

    • Country code

Figure 16-11 shows the details of the contact added.

Figure 16-11

Figure 16.11. Figure 16-11

Note

For more information about the various methods used to add a contact, refer to the ABAddressBook reference in the Apple's iPhone Reference Library. You can download the reference at: http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/ABAddressBookRef_iPhoneOS.pdf.

Removing a Contact

To remove a contact from the Contacts application, use the following code snippet:

-(void) removeContact: (NSString *) firstName andLastName:(NSString *) lastName {

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFIndex contactsCount = ABAddressBookGetPersonCount(addressBook);

    for (int i = 0; i < contactsCount; i++)
{
        ABRecordRef ref = CFArrayGetValueAtIndex(allContacts, i);
        NSString *contactFirstName = (NSString *) ABRecordCopyValue(
            ref, kABPersonFirstNameProperty);
        NSString *contactLastName = (NSString *) ABRecordCopyValue(
            ref, kABPersonLastNameProperty);

        if ( [firstName isEqualToString:contactFirstName] &&
             [lastName isEqualToString:contactLastName])
        {
            ABAddressBookRemoveRecord(addressBook, ref, nil);
            ABAddressBookSave(addressBook, nil);
        }
    }
}

The preceding method, removeContact, takes in two parameters, firstName and lastName, and searches the Contacts application for a contact with a matching first and last name. If a contact is found, it is removed from the Contacts application.

Note

For more information about the various methods used to remove a contact, refer to the ABAddressBook reference in the Apple's iPhone Reference Library. You can download the reference at http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/ABAddressBookRef_iPhoneOS.pdf.

SUMMARY

In this chapter, you saw how you could easily integrate the various built-in applications into your own iPhone applications. In particular, you saw how you can invoke the built-in SMS, Mail, Safari, and phone dialer simply by using a URL string. You also learned about accessing the Contacts and Photo Library using the classes provided by the iPhone SDK.

EXERCISES

  1. Name the various URL strings for invoking the Safari, Mail, SMS, and phone dialer applications.

  2. What is the class name for invoking the Image Picker UI in the iPhone?

  3. What is the class name for invoking the People Picker UI in the iPhone?

  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Sending email from within your application

NSString *emailString =
@"mailto:[email protected]&subject=SUBJECT&body=BODY OF EMAIL";
[[UIApplication sharedApplication]
openURL:[NSURL
URLWithString:emailString]];

Invoking Safari

[[UIApplication sharedApplication]
openURL:[NSURL
URLWithString:
@"http://www.apple.com"]];

Invoking the Phone

[[UIApplication sharedApplication]
openURL:[NSURL
URLWithString:@"tel:1234567890"]];

Invoking SMS

[[UIApplication sharedApplication]
openURL:[NSURL
URLWithString: @"sms:96924065"]];

Accessing the Photo Library

Use the UIImagePickerController class and ensure your View Controller conforms to the UINavigationControllerDelegate protocol.

Accessing the Contacts application

Use the ABPeoplePickerNavigationController class from the AddressBookUI framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset