简体   繁体   中英

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM