【发布时间】:2011-05-15 09:40:44
【问题描述】:
我正在开发一个使用 bonjour 连接到另一部 iphone 的应用程序。它的一个功能是当我连接到其他设备时,它会自动检查我是否有其他人的电话号码。所以我的问题是如何检查我的通讯录中其他设备提供的电话号码?
【问题讨论】:
标签: iphone objective-c ios addressbook
我正在开发一个使用 bonjour 连接到另一部 iphone 的应用程序。它的一个功能是当我连接到其他设备时,它会自动检查我是否有其他人的电话号码。所以我的问题是如何检查我的通讯录中其他设备提供的电话号码?
【问题讨论】:
标签: iphone objective-c ios addressbook
这是从我的地址簿方法之一中提取的示例。我没有通过电话号码进行搜索,但这让您了解如何继续满足您的需求:
- (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);
}
【讨论】:
使用这个。这是我的代码。
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");
搜索方法。它会比使用数组更快
"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;
}
【讨论】:
-(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 秒才能填充 2500 个联系人 然后您可以使用号码找到联系人
ABRecordRef ref= [contactDictionary objectForKey:@"89xxxxxxx"];
它的超快速度大约需要 0.000 倍秒
【讨论】: