如何在 iPhone 通讯录中获取联系人的电子邮件地址?我有一个应用程序,我想从通讯录中导入任何联系人以及他的所有信息,如姓名、号码、电子邮件。姓名,号码工作正常我也想获取地址。请帮助代码功能。
问问题
5087 次
1 回答
5
您应该添加框架AddressBook.framework
并添加#import <AddressBook/AddressBook.h>
.m 文件。
从通讯录中获取联系人电子邮件地址:
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;
}
于 2012-07-14T04:28:10.890 回答