0

I have written the below code for fetching contacts from address book.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id person in people){
    //fetch multiple phone nos.
    ABMultiValueRef multi = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
    for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
        NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, j);
        [_devices addObject:phone];

    }
}

But in _devices no object is adding from phone. Can anyone give me any idea where I am wrong.

iPeter
  • 1,330
  • 10
  • 26

2 Answers2

0

try this code :

ABAddressBookRef address = ABAddressBookCreate( );
CFArrayRef People = ABAddressBookCopyArrayOfAllPeople( address );
CFIndex contact = ABAddressBookGetPersonCount( address );

for ( int j = 0; j < contact; j++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex( People, j );
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
0

Please try this code

- (void)viewDidLoad
{
   [super viewDidLoad];

   phoneArray =[[NSMutableArray alloc]init];
   ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

   if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
   {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
     {
         if (granted)
         {
             [self getPhoneList];
         }
     });
   }
   else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
   {
       [self getPhoneList];
   }
}
-(void)getPhoneList
{
   [phoneArray removeAllObjects];

   ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
   int count = (int)ABAddressBookGetPersonCount(addressBook);

   CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);

   for(int i = 0;i<count;i++)
   {
       ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
       ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
     for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
     {
        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
        NSString *phoneNumber = (NSString *)phoneNumberRef;

        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@") " withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
       [phoneArray addObject:phoneNumber];
     }
 }
Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40