Create the Project
In Xcode, create a new project from the View Based Application template. Save the project as QuickStart. The next step is to add the frameworks you will need. First, go to your project window and find the target named QuickStart in the Targets group. Open its info panel (File > Get Info) and, in the General tab, you see a list of linked libraries. Add the Address Book and Address Book UI frameworks, by clicking the plus button and selecting them from the list.
Important: The project will fail to build if the Address Book and Address Book UI frameworks are not properly included.Lay Out the ViewNext, lay out the interface for your project. In the Resources folder of the project window, open the Interface Builder nib file named QuickStartViewController.xib. Add a button and two text labels to the view by dragging them in from the library. See the Figure below
project.h file#import
#import
#import
@interface QuickStartViewController : UIViewController {
IBOutlet UILabel *firstName;
IBOutlet UILabel *lastName;
}
@property (nonatomic, retain) UILabel *firstName;
@property (nonatomic, retain) UILabel *lastName;
- (IBAction)showPicker:(id)sender;
@end
project.m file
#import "QuickStartViewController.h"
@implementation QuickStartViewController
@synthesize firstName;
@synthesize lastName;
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
continue.. in same file
implementing the delegate protocol
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
NSString* name = (NSString *)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
// Do this during development
// Makes Program easier to debug
NSLog(name);
self.firstName.text = name;
[name release];
name = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.lastName.text = name;
[name release];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
// So that implementation of delgate protocol doesn't remain incomplete
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
// For handling the screen rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//Finally, release allocated memory.
- (void)dealloc {
[firstName release];
[lastName release];
[super dealloc];
}
@end
Now Easiest Part
Make connections in Interface Builder.
Open .nib file for the Project/ViewController;
Control-click (or right-click) on File’s Owner and connect the outlets for firstName and lastName from File’s Owner to the first name and last name labels. Control-click on the “Tap Me!” button and connect the Touch Up Inside outlet from the button to File’s Owner, selecting showPicker as its action.
// Now what are you waiting for? Run the application