1.获取地址簿的句柄。一定要检测系统版本否则不能获取到数据
ABAddressBookRef addressBooks = nil;
if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
addressBooks=ABAddressBookCreateWithOptions(NULL, NULL);
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool greanted, CFErrorRef error){
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
else
{
addressBooks =ABAddressBookCreate();
}
2.获取手机通讯录的联系人
//取得本地所有联系人记录
// Create addressbook data model
NSMutableArray *addressBookTemp = [NSMutableArray array];
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
3.自定义record的数据格式-- TKAddressBook
抽取联系人的有用信息重新封装
for (NSInteger i = 0; i < nPeople; i++)
{
TKAddressBook *addressBook = [[TKAddressBook alloc] init];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef abFullName = ABRecordCopyCompositeName(person);
/*
Save thumbnail image - performance decreasing
UIImage *personImage = nil;
if (person != nil && ABPersonHasImageData(person)) {
if ( &ABPersonCopyImageDataWithFormat != nil ) {
// iOS >= 4.1
CFDataRef contactThumbnailData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
personImage = [[UIImage imageWithData:(NSData*)contactThumbnailData] thumbnailImage:CGSizeMake(44, 44)];
CFRelease(contactThumbnailData);
CFDataRef contactImageData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize);
CFRelease(contactImageData);
} else {
// iOS < 4.1
CFDataRef contactImageData = ABPersonCopyImageData(person);
personImage = [[UIImage imageWithData:(NSData*)contactImageData] thumbnailImage:CGSizeMake(44, 44)];
CFRelease(contactImageData);
}
}
[addressBook setThumbnail:personImage];
*/
NSString *nameString = (NSString *)abName;
NSString *lastNameString = (NSString *)abLastName;
if ((id)abFullName != nil) {
nameString = (NSString *)abFullName;
} else {
if ((id)abLastName != nil)
{
nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
}
}
addressBook.name = nameString;
addressBook.recordID = (int)ABRecordGetRecordID(person);;
addressBook.rowSelected = NO;
ABPropertyID multiProperties[] = {
kABPersonPhoneProperty,
kABPersonEmailProperty
};
NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
ABPropertyID property = multiProperties[j];
ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
NSInteger valuesCount = 0;
if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
if (valuesCount == 0) {
CFRelease(valuesRef);
continue;
}
for (NSInteger k = 0; k < valuesCount; k++) {
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
switch (j) {
case 0: {// Phone number
addressBook.tel = [(NSString*)value telephoneWithReformat];
break;
}
case 1: {// Email
addressBook.email = (NSString*)value;
break;
}
}
CFRelease(value);
}
CFRelease(valuesRef);
}
[addressBookTemp addObject:addressBook];
[addressBook release];
if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
}
CFRelease(allPeople);
CFRelease(addressBooks);
4.利用 UILocalizedIndexedCollation 对封装的数据进行排序以a-z的顺序显示
// Sort data
UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
for (TKAddressBook *addressBook in addressBookTemp) {
NSInteger sect = [theCollation sectionForObject:addressBook
collationStringSelector:@selector(name)];
addressBook.sectionNumber = sect;
}
NSInteger highSection = [[theCollation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for (int i=0; i<=highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
[sectionArrays addObject:sectionArray];
}
for (TKAddressBook *addressBook in addressBookTemp) {
[(NSMutableArray *)[sectionArrays objectAtIndex:addressBook.sectionNumber] addObject:addressBook];
}
for (NSMutableArray *sectionArray in sectionArrays) {
NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(name)];
[_listContent addObject:sortedSection];
}