Tutorial details

Accessing Built-In Applications | App Code for Sale | Preview

Accessing Built-In Applications | iOS Tutorial

This tutorial is based on the free component Accessing Built-In Applications
Download

Overview PAGE TOP

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 Mail, Phone, Safari, SMS, Photo Library, Camera and Contacts. These applications perform most of the tasks you would expect from 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 iOS SDK. Following are the features of the product: Accessing Built-In Applications is a native iOS project Source Code for iPhone. Features:

E-Mail : allow your users to send e-mail without leaving your application using the MFMailComposeViewController class.

SMS : allow your users to send sms without leaving your application using the MFMessageComposeViewController class.

Invoking Browser : allows user to call the browser from an application.

Invoking Phone : allows user to access the phone from an application Photo Library : allows your users to photos without leaving your application using the UIImagePickerController class.

Invoking Camera : allows your users to access camera without leaving your application using the UIImagePickerController class.

Contacts : allows your users to access contacts without leaving your application using the ABPeoplePickerNavigationController class.

Add a Contacts : allows your users to access add contact without leaving your application using the ABNewPersonViewController class.

Project Set-Up: PAGE TOP

Create new project in XCode that will use single view application template and name it as ‘BuiltInApplications’. Ensure that you have the Use Automatic Reference Counting option checked as shown below.

155.png

281.png

Programming: PAGE TOP

Go to the AppDelegate.h file and add the UINavigation Controller, add property and synthesize it.

@property (strong, nonatomic) UINavigationController *navigationController;

Go to the AppDelegate.m file and modify the following code for the UINavigation Controller:

@synthesize navigationController = _navigationController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

In ViewController.m file, in ViewDidLoad add the title for the Navigation Bar:

self.title = @" Built-In Applications";

Go to ViewController.xib, change the Backgroundcolor of the view as whiteColor. Add the Scrollview to the XIB. Go to the Scrollview Content insets and change the bottom insets as 600px which means user is able to scroll horizontally. Place eight (8) UIButton in the XIB as vertically and set the title as ‘E-Mail’, ‘Invoking SMS’, ‘Invoking Browser’, ‘Invoking Phone’, ‘Photo Library’, ‘Camera’, ‘Contacts’, ‘Add Contact’ as shown below:

369.png

In ViewController.xib, add the touchUpInside methods for each buttons as shown below in .h file:

@interface ViewController : UIViewController
- (IBAction)email:(id)sender;
- (IBAction)invokingSMS:(id)sender;
- (IBAction)invokingBrowser:(id)sender;
- (IBAction)invokingPhone:(id)sender;
- (IBAction)photoLibrary:(id)sender;
- (IBAction)camera:(id)sender;
- (IBAction)contacts:(id)sender;
- (IBAction)addContact:(id)sender;

@end

Now, Click on the Project, go to Build Phases tab, in build phases go to link libraries add the MessageUI, Addressbook, AddressbookUI frameworks as shown:

477.png

Go to the ViewController.h and import all the three frameworks added above and add their delegates as shown below:

#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>
#import <AddressBookUI/AddressBookUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate,UIImagePickerControllerDelegate,ABPeoplePickerNavigationControllerDelegate,ABNewPersonViewControllerDelegate>
{
}

The above code represents:

  1. MFMailComposeViewController is framework used to integrate the Mail component in the application.

  2. MFMessageComposeViewController is framework used to integrate the Message component in the application.

  3. AddressBookUI is framework used to integrate the your contacts in the application.

  4. The MFMailComposeViewControllerDelegate protocol defines the method that your delegate must implement to manage the mail composition interface. The method of this protocol notifies your delegate object when the user has finished with the interface and is ready to dismiss it.

  5. The MFMessageComposeViewControllerDelegate protocol defines a single method that custom objects can implement to respond to updates from a message composition view (an instance of the MFMessageComposeViewController class). Use the method of this protocol to respond to the end of the user composing an SMS message. The method includes information about whether the user chose to send or cancel the message or whether the attempt to send it failed.

  6. The UIImagePickerControllerDelegate protocol defines methods that your delegate object must implement to interact with the image picker interface. The methods of this protocol notify your delegate when the user either picks an image or movie, or cancels the picker operation.

  7. The ABPeoplePickerNavigationController class (whose instances are known as people-picker navigation controllers) implements a view controller that manages a set of views that allow the user to select a contact or one of its contact-information items from an address book.

  8. The ABNewPersonViewController class (whose instances are known as new-person view controllers) implements the view controller used to create a contact. New-person view controllers are modal view controllers that manage a set of view controllers used to create a contact (ABPersonRef) and edit its properties. New-person view controllers must be used with a navigation controller in order to function properly. It is recommended that you present a new-person view controller modally.

You can integrate each of the components in your applications independent of the other. The following are the In-Built Applications:

1. Email

The MFMailComposeViewController class provides a standard interface that manages the editing and sending an email message. You can use this view controller to display a standard email view inside your application and populate the fields of that view with initial values, such as the subject, email recipients, body text, and attachments. The user can edit the initial contents you specify and choose to send the email or cancel the operation. Implement the email method:

- (IBAction)email:(id)sender 
{
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
        mailComposeVC.mailComposeDelegate = self;
        [mailComposeVC setSubject:@"Email Subject here."];
        [mailComposeVC setMessageBody:@"Email body Here" isHTML:NO];
        [self presentModalViewController:mailComposeVC animated:YES];

    }
}

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error {
    [controller dismissModalViewControllerAnimated:YES];
}

2. SMS

MFMessageComposeViewController class provides a standard system user interface for composing SMS (Short Message Service) text messages. Use this class to configure the initial recipients and body of the message, if desired, and to configure a delegate object to respond to the final result of the user’s action—whether they chose to cancel or send the message. After configuring initial values, present the view controller modally using the presentModalViewController:animated: method. When done, dismiss it using the dismissModalViewControllerAnimated: method. Implement sms method:

- (IBAction)invokingSMS:(id)sender 
{
    if([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *messageComposeVC = [[MFMessageComposeViewController alloc] init];
        messageComposeVC.messageComposeDelegate = self;
        [messageComposeVC setBody:@"This message sent from the application."];
        [self presentModalViewController:messageComposeVC animated:YES];
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                 didFinishWithResult:(MessageComposeResult)result {
    [controller dismissModalViewControllerAnimated:YES];
}

3. Invoking Browser

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:

- (IBAction)invokingBrowser:(id)sender 
{
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.chupamobile.com"]];
}

4. Invoking Phone

To make a phone call using the iPhone’s phone dialer, use the following:

- (IBAction)invokingPhone:(id)sender 
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1234567890"]];
    }

5. Photo Library

The UIImagePickerController class manages customizable, system-supplied user interfaces for taking pictures and movies on supported devices, and for choosing saved images and movies for use in your app. An image picker controller manages user interactions and delivers the results of those interactions to a delegate object. Implement the photoLibrary method:

- (IBAction)photoLibrary:(id)sender 
{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.delegate = self;
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:pickerController animated:YES];
}

6. Invoking Camera

Implement the invokingCamera method:

- (IBAction)onClickCamera:(id)sender {
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        return;

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.allowsEditing = YES;
    [self presentModalViewController:picker animated:YES];
    }

7. Contacts

ABPeoplePickerNavigationController class (whose instances are known as people-picker navigation controllers) implements a view controller that manages a set of views that allow the user to select a contact or one of its contact-information items from an address book. Implement the contact method:

- (IBAction)contacts:(id)sender 
{
    ABPeoplePickerNavigationController *peoplPickerController = [[ABPeoplePickerNavigationController alloc] init];
    NSArray * props = [NSArray arrayWithObjects:
                       [NSNumber numberWithInt:kABPersonFirstNameProperty], 
                       [NSNumber numberWithInt:kABPersonLastNameProperty ], 
                       [NSNumber numberWithInt:kABPersonEmailProperty ], nil];
    peoplPickerController.displayedProperties = props;
    peoplPickerController.peoplePickerDelegate = self;  
    [self presentModalViewController:peoplPickerController animated:YES];

}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker 
{
    [self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier 
{

    [peoplePicker setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]];
    [peoplePicker dismissModalViewControllerAnimated:YES];

    return NO;
}

8. Add Contact

The ABNewPersonViewController class (whose instances are known as new-person view controllers) implements the view controller used to create a contact. Implement the addContact method:

- (IBAction)addContact:(id)sender 
{
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
    newPersonVC.newPersonViewDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
    [self presentModalViewController:nc animated:YES];
}

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
    [self dismissModalViewControllerAnimated:YES];
}

Building and Running the Application: PAGE TOP

The final step is to build and run the application. Click on the Build and Run button located in the toolbar of the main Xcode project window. Assuming an absence of compilation errors, the application should load into the iOS Simulator environment.

1. E-Mail: - On Tap E-Mail button

125716.png

2. SMS:-2. Opens iPhone SMS, won’t work in simulator

3. Browser:-

1396.png

4. Phone : -4. Makes a call to the given no, Won’t work in iOS simulator

5. Photo Library:-

1422.png

1533.png

6. Camera : - 6. Opens camera, Won’t work in iOS simulator

7. Contacts:-

1683.png

8. Add Contact:-

1762.png

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.