The following method returns an array containing all contacts that have a given phone number. It doesn't matter if the phone number is just numbers or if it is formatted and contains a dash, etc.
This method took 0.02 seconds to search for 250 contacts on my iPhone 5 with iOS7.
#import <AddressBook/AddressBook.h> -(NSArray *)contactsContainingPhoneNumber:(NSString *)phoneNumber { /* Returns an array of contacts that contain the phone number */ // Remove non numeric characters from the phone number phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""]; // Create a new address book object with data from the Address Book database CFErrorRef error = nil; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); if (!addressBook) { return [NSArray array]; } else if (error) { CFRelease(addressBook); return [NSArray array]; } // Requests access to address book data from the user ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {}); // Build a predicate that searches for contacts that contain the phone number NSPredicate *predicate = [NSPredicate predicateWithBlock: ^(id record, NSDictionary *bindings) { ABMultiValueRef phoneNumbers = ABRecordCopyValue( (__bridge ABRecordRef)record, kABPersonPhoneProperty); BOOL result = NO; for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { NSString *contactPhoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); contactPhoneNumber = [[contactPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""]; if ([contactPhoneNumber rangeOfString:phoneNumber].location != NSNotFound) { result = YES; break; } } CFRelease(phoneNumbers); return result; }]; // Search the users contacts for contacts that contain the phone number NSArray *allPeople = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); NSArray *filteredContacts = [allPeople filteredArrayUsingPredicate:predicate]; CFRelease(addressBook); return filteredContacts; }
source share