Search Contact name from ABAddressBook using NSPredicate for iOS -
I am creating a custom phone book. In the search bar functionality, I need to find specific contacts using the text I entered. So what specific thing should I write that will filter the array using the addressbook array type ABrecord? There should be something like the name starts with "text"
You can do something like this:
NSArray * All People = CFBridgingRelease (ABAddressBookCopyArrayOfAllPeople (Address Book)); NSPredicate * predicate = [NSPredicate predicateWithBlock: ^ BOOL (ID person, NSDictionary * binding) {NSString * firstName = CFBridgingRelease (ABRecordCopyValue ((__ bridge ABRecordRef person, KABPersonFirstNameProperty)); If (first name & amp; amp; firstName rangeOfString: searchTerm option: NSCaseInsensitiveSearch] .post == 0) return TRUE; NSString * Last Name = CFBridgingRelease (ABRecordCopyValue ((__ bridge ABRecordRef person, KABPersonLastNameProperty)); If (Last Name & amp; amp; [Last Name Rangeoffstring: SearchEarth Options: NSCCINSitiv] Search Position == 0) Return Truth; // Repeat for all properties that you want to find returns FALSE; }]; NSERE * Search Results = [All people are using the Filter Array Valid: According to];
Note, if there are a lot of these searches, and you want to cancel this ABRecordCopyValue
inside the block, You can create your own custom limit once you do this for ABRecordCopyValue
for all your fields and then ABRecordCopyValue
for all of your records, and then you will see the predicateWithFormat
on your array You can use the objects (or dictionaries) with your desired keys Again and again, without the overhead of ABRecordCopyValue
, I do not believe in custom items that Apple has ever published such a key that has an array of ABRecordRef
Which you can use with predicateWithFormat
.
If you wanted to search only for the name, you can also use the ABAddressBookCopyPeopleWithName
, which completely eliminates the need for specials:
NSArray * searchResults = CFBridgingRelease (ABAddressBookCopyPeopleWithName (address book, (__bridge CF stringerf) searchTerm););
Comments
Post a Comment