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

3

10

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?

Gastronome answered 24/11, 2010 at 22:33 Comment(0)
A
18

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);
    }
Armillda answered 25/11, 2010 at 0:32 Comment(2)
AlBeebe, Unfortunetly it takes much much much more time on a device (iPhone 4, 4.2). I took 15 seconds for an addressbook of 3000)Dneprodzerzhinsk
iphone 3GS ios4.2.2 => takes more 40sec for 2000 contacts.it is not good.Ruggiero
K
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]);

}

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

Kenji answered 21/11, 2013 at 14:40 Comment(0)
R
-1

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;
}
Ruggiero answered 5/8, 2011 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.