How to search the iphone address book for a specific phone number?

久未见 提交于 2020-01-22 05:29:08

问题


I am developing an app that connects to another iphone using bonjour. One of its features is when I connect to the other device it will automatically check if I have the other persons phone number. So my problem is how do I check my address book for the phone number provided by the other device?


回答1:


Here's an example extracted from one of my address book methods. I wasn't searching by phone number but this gives you an idea have how to move forward with what you need:

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



回答2:


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

}

This takes like 0.5 sec for 2.5k contacts to fill then you can find the contact using number

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

its super fast takes like 0.000x seconds




回答3:


Use This. this is my code.

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 method. it would be faster than using array

"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;
}


来源:https://stackoverflow.com/questions/4272164/how-to-search-the-iphone-address-book-for-a-specific-phone-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!