Search This Blog

Wednesday, 12 December 2012

Address Book fetching-iOS

Hello friends,
                so here is something new,if you wana give your users an option to select a contact from contact list , here is the code then.

CODE:

First add two frameworks to your project:
1.AddressBookUI.framework
2.AddressBook.framework


Now simply in your view.header file where you want to provide this option, import below...

#import <AddressBookUI/AddressBookUI.h>

And, also add this delegate method:

ABPeoplePickerNavigationControllerDelegate  in your header file


now simply , do this whether on button click or anywhere...


   ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];











Override these below functions  as :


-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [self dismissModalViewControllerAnimated:YES];
}


-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
   
    [self displayPerson:person];
    [self dismissModalViewControllerAnimated:YES];
    return NO;
   
}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}



And here is the display person function:


-(void)displayPerson:(ABRecordRef)person{
   
       
        NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
       
       
       
       //name string value contains the first name of contact as in contact list
       
        NSString* phone = nil;
       
        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
       
        if (ABMultiValueGetCount(phoneNumbers) > 0) {
           
            phone = (__bridge_transfer NSString*)
           
            ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
           
        } else {
           
            phone = @"[None]";
           
        }
       //phone string contains the selected contact's phone number.
          
   
}



----------
Regards.


No comments:

Post a Comment