1, first get the address Book
-(void) readallpeoples
{
[contactsremoveallobjects];
abaddressbookref addressbook =abaddressbookcreatewithoptions(Nil , Nil );
dispatch_semaphore_t sema =dispatch_semaphore_create(0);
abaddressbookrequestaccesswithcompletion(addressbook, ^ (bool granted, Cferrorref Error)
{
dispatch_semaphore_signal (SEMA);
});
dispatch_semaphore_wait(Sema,dispatch_time_forever);
cfarrayref results =abaddressbookcopyarrayofallpeople(addressbook);
for (int i =0; i < cfarraygetcount(results); i++)
{
abrecordref person = Cfarraygetvalueatindex(results, i);
nsstring *lastname = (__bridgensstring*)abrecordcopyvalue (person,kabpersonlastnameproperty );
NSLog (@ "lastname:%@", LastName);
abmultivalueref phone =abrecordcopyvalue(person,kabpersonphoneproperty);
// Read Contact information
for (int k =0; k<abmultivaluegetcount(phone); k++)
{
nsstring * personphone = (__bridgensstring*)abmultivaluecopyvalueatindex(phone , k);
if (LastName = = Nil) {
LastName =@ "";
}
if (Person = = Nil) {
continue;
}
// This sentence is a storage structure, contacts is nsmutablearray*
[contactsaddobject:@[lastname,personphone]];
}
}
Cfrelease (results);
Cfrelease (AddressBook);
[self. TableView reloaddata];
}
2, the name (LastName) and mobile phone number (personphone) fuzzy query
NSPredicate *predicate = [nspredicate predicatewithformat:@ "self[0] contains [CD]%@| | SELF[1] contains [CD]%@ ", user input String, user input string];
filterdata = [[nsarrayalloc] initwitharray: [ contacts filteredarrayusingpredicate :p redicate]];
Filterdata is the result of the query.
Fuzzy query for IOS address Book