Address Book Integration

[button colour=”accent” Align=”center” type=”roundedarrow” size=”large” link=”https://www.code-brew.com/wp-content/uploads/2013/03/AddressBookIntegration.zip” target=”_blank”]Download Source[/button]

Address book integration is not that much difficult , but the one difficulty that i faced when i was using this in my code was to get the address . The first name , Last name were pretty much straight forward . But retrieving address was a bit complicated , so i decided to make this tutorial . But before doing this you should read Apple ‘s guide AddressBook Programming Guide for iPhone

STEP 1 Create an interface as shown.

Address book integration

STEP 2

Now we begin with (ViewController.h) and write the following code.

[csharp]</p> <p>#import <UIKit/UIKit.h></p> <p>#import <AddressBook/AddressBook.h></p> <p>#import <AddressBookUI/AddressBookUI.h></p> <p>@interface ViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate></p> <p>- (IBAction)contacts:(id)sender;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *fName;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *lName;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *Phnumber;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *city;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *state;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *street;</p> <p>@property (retain, nonatomic) IBOutlet UILabel *emailID;</p> <p>@end</p> <p>[/csharp]

 

    1. Import two frameworks AddressBook and AddressBookUI .

 

    1. Create Seven outlets for First Name ,Last Name ,Phone No ,EMail ID , City ,State ,Street .

 

    1. Create an action When CLICKED on button “GET”.

 

Thats it for ViewController.h

STEP 3

  1. Now in (ViewController.m ) Now synthesize the properties we created. And we write code in the action of the button.

[csharp]</p> <p>@synthesize fName,lName,Phnumber,emailID,city,state,street;</p> <p>- (IBAction)contacts:(id)sender {</p> <p>ABPeoplePickerNavigationController *people_picker =[[ABPeoplePickerNavigationController alloc]init];</p> <p>people_picker.peoplePickerDelegate=self;</p> <p>[self presentModalViewController:people_picker animated:YES];</p> <p>//[people_picker release];</p> <p>}</p> <p>[/csharp]

 

    1. We create a ABPeoplePickerNavigationController , we allocate some space to it and initialise it .

 

    1. There is a need to call the delegates of people Picker. Presenting the ABPeoplePickerNavigationController and animating it to YES .

 

#pragma mark ABPeoplePickerNavigationController DELEGATES

[csharp]</p> <p>- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{</p> <p>[self dismissModalViewControllerAnimated:YES];}</p> <p>[/csharp]

On clicking the Cancel button the presentation mode should be dismissed . The above code is for that .

[csharp]</p> <p>-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePickershouldContinueAfterSelectingPerson:(ABRecordRef)person {</p> <p>NSString *name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);</p> <p>self.fName.text = name;</p> <p>[name release];</p> <p>name = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);</p> <p>self.lName.text = name;</p> <p>[name release];</p> <p>//How To get Phone Number</p> <p>NSString* phone = nil;</p> <p>ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);</p> <p>if (ABMultiValueGetCount(phoneNumbers) > 0) {</p> <p>phone = (NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);</p> <p>} else {</p> <p>phone = @"[None]";</p> <p>}</p> <p>self.Phnumber.text =phone;</p> <p>CFRelease(phoneNumbers);</p> <p>//How To get Email Id</p> <p>NSString* email = nil;</p> <p>ABMultiValueRef emails =ABRecordCopyValue(person, kABPersonEmailProperty);</p> <p>if (ABMultiValueGetCount(emails) > 0) {</p> <p>email = (NSString*) ABMultiValueCopyValueAtIndex(emails, 0);</p> <p>}</p> <p>else {</p> <p>email = @"[None]";</p> <p>}</p> <p>self.emailID.text =email;</p> <p>CFRelease(emails);</p> <p>return YES;</p> <p>}</p> <p>[/csharp]

    1. We created a string that will hold the the value returned by function called ABRrecordCopyValue that has two parameters the PERSON itself and the FirstNameProperty and setting the value of corresponding outlet.

 

    1. We created a string that will hold the the value returned by function called ABRrecordCopyValue that has two parameters the PERSON itself and the LastNameProperty and setting the value of corresponding outlet.

 

    1. Next since phone no is a multi value property , we need to treat is special .We need to create a ” multi value ref “and copy the values into it.And if there are more than one phone numbers we will consider only the first value.

 

    1. Lastly we will declare a string to store the returned value.

 

    1. And finally assign the corresponding outlet to its value. The same goes for Email ID.

 

[csharp]<br /> -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePickershouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {</p> <p>// Only inspect the value if it’s an address.</p> ne difficulty that i faced when i was using this in my code was to get the address <p>if (property == kABPersonAddressProperty) {</p> <p>/*</p> <p>* Set up an ABMultiValue to hold the address values; copy from address</p> <p>* book record.</p> <p>*/</p> <p>ABMultiValueRef multi = ABRecordCopyValue(person, property);</p> <p>// Set up an NSArray and copy the values in.</p> <p>NSArray *theArray = [(id)</p> <p>ABMultiValueCopyArrayOfAllValues(multi) autorelease];</p> <p>NSLog(@"%@",theArray); //ARRAY OF DICTIONARY</p> <p>// Figure out which values we want and store the index.</p> <p>const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi , identifier);</p> <p>NSLog(@"%@",theIndex);</p> <p>// Set up an NSDictionary to hold the contents of the array.</p> <p>NSDictionary *theDict = [theArray objectAtIndex:theIndex];</p> <p>NSLog(@"%@",theDict);</p> <p>NSString *city , *state , *street;</p> <p>city = [NSString stringWithFormat:@"%@ ,<span style="font-size: 13px; line-height: 19px;"> </span>[theDict objectForKey:(NSString *)kABPersonAddressCityKey]];</p> <p>state= [NSString stringWithFormat:@"%@ ,[theDict objectForKey:(NSString *)kABPersonAddressStateKey]];</p> <p>self.city.text = city;</p> <p>self.state.text =state;</p> <p>self.street.text =street ;</p> <p>// Return to the main view controller.</p> <p>[ self dismissModalViewControllerAnimated:YES ];</p> <p>return NO;</p> <p>}</p> <p>// If they didn’t pick an address, return YES here to keep going.</p> <p>return YES;</p> <p>}</p> <p>@end</p> <p>[/csharp]

    1. Firstly we need to define ABMultivalue Ref and copy the selected contents into it .

 

    1. Next we need to define an Array that will hold all the copied value .There is a dilemma which one we need ?

 

    1. Every address have an unique identifier , and we can reference them by index when we want to extract them from array. So we create theIndex and assign the returned value of the
      ABMultiValueGetIndexForIdentifier
      function to it .

 

    1. Now we know which value of array we need , so extract the value from array and put in the Dictionary.

 

    1. So Next we make an NSDictionary, theDict.

 

    1. Lastly we will assign the corresponding NSString ‘s to the corresponding OUTLETS.

 

NOTE ARRAY here is an array of DICTIONARIES. A person can have more than one address so the each object of an array is a dictionary and each dictionary object is an address.

List of contacts appearing when clicked on button made by us

List of contacts appearing when clicked on button made by us

Contact details

Contact details

 

POST COMMENT FOR ANY iOS RELATED PROBLEM

 

THANK YOU !

By | 2018-02-16T10:29:10+00:00 March 24th, 2013|How it works|0 Comments

About the Author:

Leave A Comment