Xcode从地址簿中获取联系人电子邮件地址

时间:2012-07-14 04:16:00

标签: iphone addressbook

如何在iPhone地址簿中获取联系人的电子邮件地址? 我有一个应用程序,我想从地址簿中导入任何联系人,包括姓名,号码,电子邮件等所有信息。 姓名,号码工作正常我也想提取地址。 请帮助解决代码功能。

1 个答案:

答案 0 :(得分:5)

您应该添加框架作品AddressBook.framework并在.m文件中添加#import <AddressBook/AddressBook.h>

从地址簿中获取联系电子邮件地址::

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

我认为这会对你有所帮助。

编辑::

您应该使用委托- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person方法。当您选择联系人列表中的任何人时,此方法将调用。试试这个...

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