如何在iphone地址簿中搜索特定的电话号码?

Ada*_*mou 10 iphone objective-c addressbook ios

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

Joh*_*ter 18

这是从我的一种地址簿方法中提取的示例.我没有通过电话号码搜索,但这可以让您了解如何向前推进您的需求:

- (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);
    }
Run Code Online (Sandbox Code Playgroud)

  • AlBeebe,不幸的是它在设备上需要花费更多的时间(iPhone 4,4.2).我用了15秒钟获得了3000的地址簿 (2认同)

Roh*_*hit 5

-(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]);

}
Run Code Online (Sandbox Code Playgroud)

这需要0.5秒才能填充2.5k触点,然后您可以使用编号找到联系人

 ABRecordRef ref= [contactDictionary objectForKey:@"89xxxxxxx"];
Run Code Online (Sandbox Code Playgroud)

它的超快速度需要0.000x秒