user1500683
user1500683

Reputation: 41

Xcode to fetch contact email address from addressbook

How can I fetch email address of a contact in iPhone address book? I have an app in which I want to import any contact from address book with all his information like name,number,email. Name,number is working fine I want to fetch address too. Please help with the code function.

Upvotes: 4

Views: 5106

Answers (1)

Prasad G
Prasad G

Reputation: 6718

You should add frame work AddressBook.framework and add #import <AddressBook/AddressBook.h> in .m file.

To fetch contact email address from address book::

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
    ABRecordRef person = CFArrayGetValueAtIndex(people, i);
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
        NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(emails, j);
        [allEmails addObject:email];
        [email release];
    }
    CFRelease(emails);
}
NSLog(@"All Mails:%@",allEmails);
CFRelease(addressBook);
CFRelease(people);

I think it will be helpful to you.

Edit::

You should use delegate - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person method. When you select any person in contact list this method will call. Try this one...

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

ABMultiValueRef  emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".
NSLog(@"Email:: %@",emailId);

    return YES;
}

Upvotes: 5

Related Questions