如何searchiPhone通讯簿中的特定电话号码?

我正在开发一个应用程序,连接到另一个iPhone使用bonjour。 其中一个特点是当我连接到其他设备时,它会自动检查我是否有其他人的电话号码。 所以我的问题是如何检查我的地址簿中的其他设备提供的电话号码?

这是一个从我的地址簿方法中提取的例子。 我不是通过电话号码search,但是这给了你一个想法,有如何前进,你需要什么:

- (void) scanAddressBookSample { NSUInteger i; NSUInteger k; ABAddressBookRef addressBook = ABAddressBookCreate(); NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook); if ( people==nil ) { NSLog(@"NO ADDRESS BOOK ENTRIES TO SCAN"); CFRelease(addressBook); return; } for ( i=0; i<[people count]; i++ ) { ABRecordRef person = (ABRecordRef)[people objectAtIndex:i]; // // Phone Numbers // ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers ); for ( k=0; k<phoneNumberCount; k++ ) { CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k ); CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k ); CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel ); // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile" // Find the ones you want here // NSLog(@"-----PHONE ENTRY -> %@ : %@", phoneNumberLocalizedLabel, phoneNumberValue ); CFRelease(phoneNumberLocalizedLabel); CFRelease(phoneNumberLabel); CFRelease(phoneNumberValue); } } [people release]; CFRelease(addressBook); } 
 -(void)createQuickAccessContacts{ NSMutableDictionary contactDictionary= [[NSMutableDictionary alloc]init]; CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex n = ABAddressBookGetPersonCount(addressBook); NSDate *date=[NSDate date]; for( int i = 0 ; i < n ; i++ ) { ABRecordRef ref = CFArrayGetValueAtIndex(all, i); ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty); for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); NSString *phoneNumber = (__bridge NSString *)phoneNumberRef; [contactDictionary setObject:(__bridge id)(ref) forKey:phoneNumber]; } } NSLog(@" Time taken %f for %i contacts",[[NSDate date] timeIntervalSinceDate:date],[contactDictionary count]); } 

这需要0.5秒2.5k的接触填充,然后你可以find联系人使用数字

  ABRecordRef ref= [contactDictionary objectForKey:@"89xxxxxxx"]; 

它的超级快速需要0.000x秒

用这个。 这是我的代码。

 NSLog(@"=====Make People Array with Numbers. Start."); peopleWithNumber = [[NSMutableDictionary alloc] init]; for (int i=0; i < [people count]; i++) { NSInteger phoneCount = [self phoneCountAtIndex:i]; if (phoneCount != 0) { NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; for (int j=0 ; j < phoneCount ; j++) { [phoneNumbers addObject:[self phoneNumberAtIndex:i phoneIndex:j]]; } [peopleWithNumber addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithArray:phoneNumbers], [self fullNameAtIndex:i], nil]]; } } NSLog(@"=====Make People Array with Numbers. End.\n"); 

search方法。 这将比使用数组更快

“NSArray * people =(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);”

 - (NSArray *)searchNamesByNumber:(NSString *)number { NSString *predicateString = [NSString stringWithFormat:@"%@[SELF] contains '%@'",@"%@",number]; NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:predicateString,peopleWithNumber,number]; NSArray *names = [[peopleWithNumber allKeys] filteredArrayUsingPredicate:searchPredicate]; return names; }