Hi everyone,
This will help you to fetch contact information from your iOS Device Address book. First you have to include "AddressBookUI.framework" into your project and then you have to include "UINavigationControllerDelegate","ABPeoplePickerNavigationControllerDelegate" into header part of the controller. You have to also import "#import <AddressBookUI/AddressBookUI.h>" into current controller's header part.
#pragma mark -
#pragma mark Button Action Event
-(IBAction)callAddressbook:(id)sender
{
ABPeoplePickerNavigationController *addressBookController = [[ABPeoplePickerNavigationController alloc]init];
addressBookController.peoplePickerDelegate = self;
addressBookController.delegate = self;
addressBookController.navigationBar.tintColor = [UIColor blackColor];
addressBookController.searchDisplayController.searchBar.tintColor = [UIColor blackColor];
addressBookController.searchDisplayController.searchBar.backgroundColor = [UIColor blackColor];
[self presentModalViewController:addressBookController animated:YES];
[addressBookController release];
addressBookController = nil;
}
#pragma mark -
#pragma mark ABPeoplePickerNavigationController Delegate Method
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker // called when address book closed
{
[peoplePicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker // called when select any contact from address book
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
ABMultiValueRef addresses = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonAddressProperty) autorelease];
NSArray *addressesArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(addresses) autorelease];
NSDictionary *dictAddress = [addressesArray objectAtIndex:0];
ABMultiValueRef phone = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty) autorelease];
NSArray *phoneArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(phone) autorelease];
NSMutableString *strPhone = [NSMutableString string];
for (int i=0; i<[phoneArray count]; i++)
{
[strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]];
}
[ApplicationData sharedInstance].selectedContact.contactId = (NSInteger) ABRecordGetRecordID(person);
[ApplicationData sharedInstance].selectedContact.firstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)autorelease];
[ApplicationData sharedInstance].selectedContact.lastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease];
[ApplicationData sharedInstance].selectedContact.street = [dictAddress valueForKey:@"Street"]==nil?@"":[dictAddress valueForKey:@"Street"];
[ApplicationData sharedInstance].selectedContact.telephone = [strPhone stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
[ApplicationData sharedInstance].selectedContact.city = [dictAddress valueForKey:@"City"]==nil?@"":[dictAddress valueForKey:@"City"];
[ApplicationData sharedInstance].selectedContact.postcode = [dictAddress valueForKey:@"ZIP"]==nil?@"":[dictAddress valueForKey:@"ZIP"];
[ApplicationData sharedInstance].selectedContact.state = [dictAddress valueForKey:@"State"]==nil?@"":[dictAddress valueForKey:@"State"];
[ApplicationData sharedInstance].selectedContact.country = [dictAddress valueForKey:@"CountryCode"]==nil?@"":[dictAddress valueForKey:@"CountryCode"];
[ApplicationData sharedInstance].obj_sendOrder.recipientName = [NSString stringWithFormat:@"%@ %@",[ApplicationData sharedInstance].selectedContact.firstName,[ApplicationData sharedInstance].selectedContact.lastName];
[ApplicationData sharedInstance].obj_sendOrder.street = [ApplicationData sharedInstance].selectedContact.street;
[ApplicationData sharedInstance].obj_sendOrder.city = [ApplicationData sharedInstance].selectedContact.city;
[ApplicationData sharedInstance].obj_sendOrder.zip = [ApplicationData sharedInstance].selectedContact.postcode;
[ApplicationData sharedInstance].obj_sendOrder.state = [ApplicationData sharedInstance].selectedContact.state;
[ApplicationData sharedInstance].obj_sendOrder.country = [ApplicationData sharedInstance].selectedContact.country;
[ApplicationData sharedInstance].obj_sendOrder.contactType = @"Phone";
dictAddress = nil;
addresses = nil;
addressesArray = nil;
phone = nil;
phoneArray = nil;
strPhone = nil;
[peoplePicker dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier // called to show detail of contact
{
return NO;
}
Still if you have questions regarding this then you can ask me anytime.
Thanks & Regards,
Nilesh Prajapati
This will help you to fetch contact information from your iOS Device Address book. First you have to include "AddressBookUI.framework" into your project and then you have to include "UINavigationControllerDelegate","ABPeoplePickerNavigationControllerDelegate" into header part of the controller. You have to also import "#import <AddressBookUI/AddressBookUI.h>" into current controller's header part.
#pragma mark -
#pragma mark Button Action Event
-(IBAction)callAddressbook:(id)sender
{
ABPeoplePickerNavigationController *addressBookController = [[ABPeoplePickerNavigationController alloc]init];
addressBookController.peoplePickerDelegate = self;
addressBookController.delegate = self;
addressBookController.navigationBar.tintColor = [UIColor blackColor];
addressBookController.searchDisplayController.searchBar.tintColor = [UIColor blackColor];
addressBookController.searchDisplayController.searchBar.backgroundColor = [UIColor blackColor];
[self presentModalViewController:addressBookController animated:YES];
[addressBookController release];
addressBookController = nil;
}
#pragma mark -
#pragma mark ABPeoplePickerNavigationController Delegate Method
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker // called when address book closed
{
[peoplePicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker // called when select any contact from address book
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
ABMultiValueRef addresses = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonAddressProperty) autorelease];
NSArray *addressesArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(addresses) autorelease];
NSDictionary *dictAddress = [addressesArray objectAtIndex:0];
ABMultiValueRef phone = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty) autorelease];
NSArray *phoneArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(phone) autorelease];
NSMutableString *strPhone = [NSMutableString string];
for (int i=0; i<[phoneArray count]; i++)
{
[strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]];
}
[ApplicationData sharedInstance].selectedContact.contactId = (NSInteger) ABRecordGetRecordID(person);
[ApplicationData sharedInstance].selectedContact.firstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)autorelease];
[ApplicationData sharedInstance].selectedContact.lastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease];
[ApplicationData sharedInstance].selectedContact.street = [dictAddress valueForKey:@"Street"]==nil?@"":[dictAddress valueForKey:@"Street"];
[ApplicationData sharedInstance].selectedContact.telephone = [strPhone stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
[ApplicationData sharedInstance].selectedContact.city = [dictAddress valueForKey:@"City"]==nil?@"":[dictAddress valueForKey:@"City"];
[ApplicationData sharedInstance].selectedContact.postcode = [dictAddress valueForKey:@"ZIP"]==nil?@"":[dictAddress valueForKey:@"ZIP"];
[ApplicationData sharedInstance].selectedContact.state = [dictAddress valueForKey:@"State"]==nil?@"":[dictAddress valueForKey:@"State"];
[ApplicationData sharedInstance].selectedContact.country = [dictAddress valueForKey:@"CountryCode"]==nil?@"":[dictAddress valueForKey:@"CountryCode"];
[ApplicationData sharedInstance].obj_sendOrder.recipientName = [NSString stringWithFormat:@"%@ %@",[ApplicationData sharedInstance].selectedContact.firstName,[ApplicationData sharedInstance].selectedContact.lastName];
[ApplicationData sharedInstance].obj_sendOrder.street = [ApplicationData sharedInstance].selectedContact.street;
[ApplicationData sharedInstance].obj_sendOrder.city = [ApplicationData sharedInstance].selectedContact.city;
[ApplicationData sharedInstance].obj_sendOrder.zip = [ApplicationData sharedInstance].selectedContact.postcode;
[ApplicationData sharedInstance].obj_sendOrder.state = [ApplicationData sharedInstance].selectedContact.state;
[ApplicationData sharedInstance].obj_sendOrder.country = [ApplicationData sharedInstance].selectedContact.country;
[ApplicationData sharedInstance].obj_sendOrder.contactType = @"Phone";
dictAddress = nil;
addresses = nil;
addressesArray = nil;
phone = nil;
phoneArray = nil;
strPhone = nil;
[peoplePicker dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier // called to show detail of contact
{
return NO;
}
Still if you have questions regarding this then you can ask me anytime.
Thanks & Regards,
Nilesh Prajapati
Superb its very helpful
ReplyDeleteThanks for using it ...
DeleteHopefully, you shall get useful information from the blog.
Best Regards,
Nilesh M. Prajapati
what is ApplicationData??
ReplyDeleteHi,
DeleteSorry for delay...
"ApplicationData" is singleton that i used in my above code. Generally, we use it for sharing values throughout the application. You can get detail information about singleton by searching in google.
Best regards,
Nilesh M. Prajapati