how to get contacts in to our native application from Addressbook

这一生的挚爱 提交于 2019-12-08 12:59:42

问题


i used below code for getting contacts in to our application.

this code is working fine in simulator in device allPeople getting Zero

any thing i forgot that one pls help me

if(addressBookCollection==nil) {
    addressBookCollection = ABAddressBookCreate();
 }
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookCollection);
CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering (addressBookCollection,source,1);

CFIndex nPeople = ABAddressBookGetPersonCount(addressBookCollection);

NSMutableArray* tempContactArray = [[NSMutableArray alloc] initWithCapacity:nPeople];
    for(int i=0 ; i < nPeople; i++)
    {
                  ......
        }

回答1:


 #import<AddressBook/AddressBook.h> 

 ABAddressBookRef addressBook = ABAddressBookCreate();

 CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook); 

 CFIndex n = ABAddressBookGetPersonCount(addressBook); 

 for( int i = 0 ; i < n ; i++ ) 

   { 

      ABRecordRef ref = CFArrayGetValueAtIndex(all, i); 

      ABMultiValueRef contactnumber = ABRecordCopyValue(ref, kABPersonPhoneProperty);

      for(CFIndex j = 0; j < ABMultiValueGetCount(contactnumber); j++)

          { 

               CFStringRef contactnumberRef = ABMultiValueCopyValueAtIndex(contactnumber, j);

               NSString *contactnumberstr = (NSString *)contactnumberRef;

               CFRelease(contactnumberRef); [self.contactnumberArray contactnumber]; 

               [contactnumber release]; 
          } 
   }

you will get the contacts in array of "self.contactnumberArray".

Enjoy!



来源:https://stackoverflow.com/questions/8229901/how-to-get-contacts-in-to-our-native-application-from-addressbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!