1. Add addressbook Library
-(ibaction) Add: (ID) Sender {
abaddressbookrequestaccesswithcompletion(abaddressbookref addressbookref , ^ (bool Granted,cferrorref error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
Nsarray * array=[selfgetcontactsfromaddressbook];
});
}Else {
//todo:show Alert
}
});
}
-(nsmutablearray *) Getcontactsfromaddressbook
{
cferrorref error = NULL;
nsmutablearray * contacts = [[nsmutablearrayalloc]init];
abaddressbookref addressbook = abaddressbookcreatewithoptions(NULL, &error);
if (AddressBook) {
nsarray *allcontacts = (__bridge_transfernsarray*) Abaddressbookcopyarrayofallpeople(AddressBook);
nsmutablearray *mutablecontacts = [nsmutablearrayarraywithcapacity: allcontacts. Count];
Nsuinteger i = 0;
for (i = 0; i<[allcontactsCount]; i++)
{
thcontact a model object with name and Phonenum two properties
thcontact *contact = [[thcontactalloc] init];
abrecordref ContactPerson = (__bridgeabrecordref) allcontacts[i];
Contact. recordId =abrecordgetrecordid(ContactPerson);
//Get first and last names
nsstring *firstname = (__bridge_transfernsstring*)abrecordcopyvalue( ContactPerson,kabpersonfirstnameproperty);
nsstring *lastname = (__bridge_transfernsstring*)abrecordcopyvalue( ContactPerson,kabpersonlastnameproperty);
nsstring * midname = (__bridge_transfernsstring*)abrecordcopyvalue( ContactPerson,kabpersonmiddlenameproperty);
//Set Contact Properties
Contact. firstName = firstName;
Contact. lastName = lastName;
Contact. middlename = midname;
//Get mobile number
abmultivalueref phonesref = abrecordcopyvalue(ContactPerson, kabpersonphoneproperty);
Contact. phone = [selfgetmobilephoneproperty:p honesref];
if (PHONESREF) {
Cfrelease (PHONESREF);
}
if (Contact. phone. count >0) {
[Mutablecontactsaddobject: contact];
}
}
if (AddressBook) {
Cfrelease (AddressBook);
}
contacts = [nsmutablearrayarraywitharray: mutablecontacts];
return contacts;
}
Else
{
NSLog (@ "Error");
}
return nil;
}
-(nsmutablearray *) Getmobilephoneproperty: (abmultivalueref) phonesref
{
nsmutablearray * array = [nsmutablearrayarray];
for (int k =0; k<abmultivaluegetcount(phonesref); k++)
{
// get phone Label
//NSString * Personphonelabel = (__bridge nsstring*) Abaddressbookcopylocalizedlabel (ABMULTIVALUEC Opylabelatindex (Phonesref, k));
// get The phone value under the Label
nsstring * personphone = (__bridgensstring*)Abmultivaluecopyvalueatindex( Phonesref, K);
if (Personphone) {
[Arrayaddobject:p Ersonphone];
}
}
return array;
}
IOS Get Contacts