user3324336
user3324336

Reputation: 51

NSPredicate with CNContact not working

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

Answers (1)

Annie Dev
Annie Dev

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

Related Questions