objective c - Retrieve all contacts phone numbers in iOS -


so far saw methods multiple phone numbers if show picker user can select people , phone number. want retrieving contacts' numbers. possible?

try works ios 6 ios 5.0 or older:

sample project demo

first add following frameworks in link binary libraries

  • addressbookui.framework
  • addressbook.framework

then import

#import <addressbook/abaddressbook.h> #import <addressbookui/addressbookui.h> 

then use following code

requesting permission access address book

abaddressbookref addressbook = abaddressbookcreatewithoptions(null, null);  __block bool accessgranted = no;  if (&abaddressbookrequestaccesswithcompletion != null) { // on ios 6     dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);      abaddressbookrequestaccesswithcompletion(addressbook, ^(bool granted, cferrorref error) {         accessgranted = granted;         dispatch_semaphore_signal(semaphore);     });      dispatch_semaphore_wait(semaphore, dispatch_time_forever);     dispatch_release(semaphore); }  else { // on ios 5 or older     accessgranted = yes;     [self getcontactswithaddressbook:addressbook]; }  if (accessgranted) {     [self getcontactswithaddressbook:addressbook]; } 

retrieving contacts addressbook

// contacts. - (void)getcontactswithaddressbook:(abaddressbookref )addressbook {      contactlist = [[nsmutablearray alloc] init];     cfarrayref allpeople = abaddressbookcopyarrayofallpeople(addressbook);     cfindex npeople = abaddressbookgetpersoncount(addressbook);      (int i=0;i < npeople;i++) {         nsmutabledictionary *dofperson=[nsmutabledictionary dictionary];          abrecordref ref = cfarraygetvalueatindex(allpeople,i);          //for username , surname         abmultivalueref phones =(__bridge abmultivalueref)((__bridge nsstring*)abrecordcopyvalue(ref, kabpersonphoneproperty));          cfstringref firstname, lastname;         firstname = abrecordcopyvalue(ref, kabpersonfirstnameproperty);         lastname  = abrecordcopyvalue(ref, kabpersonlastnameproperty);         [dofperson setobject:[nsstring stringwithformat:@"%@ %@", firstname, lastname] forkey:@"name"];          //for email ids         abmutablemultivalueref email  = abrecordcopyvalue(ref, kabpersonemailproperty);         if(abmultivaluegetcount(email) > 0) {             [dofperson setobject:(__bridge nsstring *)abmultivaluecopyvalueatindex(email, 0) forkey:@"email"];          }          //for phone number         nsstring* mobilelabel;          for(cfindex j = 0; j < abmultivaluegetcount(phones); j++) {             mobilelabel = (__bridge nsstring*)abmultivaluecopylabelatindex(phones, j);             if([mobilelabel isequaltostring:(nsstring *)kabpersonphonemobilelabel])             {                 [dofperson setobject:(__bridge nsstring*)abmultivaluecopyvalueatindex(phones, j) forkey:@"phone"];             }             else if ([mobilelabel isequaltostring:(nsstring*)kabpersonphoneiphonelabel])             {                 [dofperson setobject:(__bridge nsstring*)abmultivaluecopyvalueatindex(phones, j) forkey:@"phone"];                 break ;             }          }     [contactlist addobject:dofperson];      } nslog(@"contacts = %@",contactlist); } 

to retrive other information

// personal information properties kabpersonfirstnameproperty;          // first name - kabstringpropertytype kabpersonlastnameproperty;           // last name - kabstringpropertytype kabpersonmiddlenameproperty;         // middle name - kabstringpropertytype kabpersonprefixproperty;             // prefix ("sir" "duke" "general") - kabstringpropertytype kabpersonsuffixproperty;             // suffix ("jr." "sr." "iii") - kabstringpropertytype kabpersonnicknameproperty;           // nickname - kabstringpropertytype kabpersonfirstnamephoneticproperty;  // first name phonetic - kabstringpropertytype kabpersonlastnamephoneticproperty;   // last name phonetic - kabstringpropertytype kabpersonmiddlenamephoneticproperty; // middle name phonetic - kabstringpropertytype kabpersonorganizationproperty;       // company name - kabstringpropertytype kabpersonjobtitleproperty;           // job title - kabstringpropertytype kabpersondepartmentproperty;         // department name - kabstringpropertytype kabpersonemailproperty;              // email(s) - kabmultistringpropertytype kabpersonbirthdayproperty;           // birthday associated person - kabdatetimepropertytype kabpersonnoteproperty;               // note - kabstringpropertytype kabpersoncreationdateproperty;       // creation date (when first saved) kabpersonmodificationdateproperty;   // last saved date  // address information properties kabpersonaddressproperty;            // street address - kabmultidictionarypropertytype kabpersonaddressstreetkey; kabpersonaddresscitykey; kabpersonaddressstatekey; kabpersonaddresszipkey; kabpersonaddresscountrykey; kabpersonaddresscountrycodekey; 

further reference read apple docs

update: need add description why need access contacts in apps-info.plist

privacy - contacts usage description

or

<key>nscontactsusagedescription</key> <string>write reason why app needs contact.</string> 

for getting user image.

uiimage *contactimage; if(abpersonhasimagedata(ref)){  contactimage = [uiimage imagewithdata:(__bridge nsdata *)abpersoncopyimagedata(ref)]; } 

note:

the addressbook framework deprecated in ios 9 , replaced new , improved contacts framework


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -