如何从ABAdressBook中找到包含10 000个联系人的单个号码?
目前我正在使用ABAddressBookRef。是否有另一种减少迭代的方法?
我的代码段:
if([numberType intValue] == NUMBER_TYPE_EXTERNAL && [appDelegate isNativeContactAccess] == YES)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(numberType == %@)",[NSNumber numberWithInt:NUMBER_TYPE_EXTERNAL]];
[fetchRequest setPredicate:predicate];
NSArray *results = [context executeFetchRequest:fetchRequest error:&error];
/* The method ABAddressBookCreate() is deprecated in ios 6 so below method is used here */
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
for (NSInteger resultCnt=0; resultCnt<[results count]; resultCnt++)
{
Contact *contact = [results objectAtIndex:resultCnt];
ABRecordID r = [contact.indexValue intValue];
ABRecordRef fetchData = ABAddressBookGetPersonWithRecordID(addressBook, r);
if(fetchData != nil)
{
NSUInteger lengthOfSearchStr = [searchNumber length];
ABMultiValueRef multi = ABRecordCopyValue(fetchData, kABPersonPhoneProperty);
NSUInteger j=0;
for(j=0; j<ABMultiValueGetCount(multi); j++)
{
NSString *getNum = (NSString *)ABMultiValueCopyValueAtIndex(multi, j);
NSString *removedspecialchar6 = [getNum stringByReplacingOccurrencesOfString:@"[^0-9*+#]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0,[getNum length])];
if(lengthOfSearchStr <= [removedspecialchar6 length])
{
number = [removedspecialchar6 substringFromIndex:[removedspecialchar6 length] - lengthOfSearchStr];
if([number isEqualToString:searchNumber])
{
[getNum release];
CFRelease(addressBook);
CFRelease(multi);
return contact;
}
}
[getNum release];
}
CFRelease(multi);
}
}
CFRelease(addressBook);
}
答案 0 :(得分:0)
NSUInteger index;
NSUInteger findIndex;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *constactsNo = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
if ( constactsNo ==nil )
{
CFRelease(addressBook);
return;
}
for ( index=0; index<[constactsNo count]; index++ )
{
ABRecordRef find = (ABRecordRef)[constactsNo objectAtIndex:index];
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(find, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
for ( findIndex =0; findIndex <phoneNumberCount; findIndex ++ )
{
CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, findIndex );
CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, findIndex );
CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberLabel);
CFRelease(phoneNumberValue);
}
}
[constactsNo release];
CFRelease(addressBook);