Reputation: 51
Hi I'm trying to filter my contact, all is well in the simulator xcode, but when I try it on my iPhone I have a problem with regular expression predicate. Any suggestions ?. Thank you very much.
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredContacs.removeAll(keepCapacity: false)
let searchPredcate = NSPredicate(format: "givenName contains[c] %@ OR familyName contains[c] %@",searchController.searchBar.text!)
let array = (self.contacts as NSArray).filteredArrayUsingPredicate(searchPredcate)
self.filteredContacs = array as! [CNContact]
self.tableViewContacts.reloadData()
}
Upvotes: 1
Views: 521
Reputation: 282
You can use CNContact Predicates for that.
- (NSArray*)searchContactsMatchingName:(NSString*)pSearchName andKeysToFetch:(NSArray*)pKeysToFetch
{
NSError *pError = nil;
NSPredicate *pPredicate = [CNContact predicateForContactsMatchingName:pSearchName];
CNContactStore *pStore = [[CNContactStore alloc] init];
NSArray *pResults = [pStore unifiedContactsMatchingPredicate:pPredicate keysToFetch:pKeysToFetch error:&pError];
return pResults;
}
Upvotes: 1