-1

I faced one problem from the address book,i saved two phone numbers from my contacts one as home other one as office,i am getting only one number in ,application contact list.if i want to get the two numbers what i want to do.

1 Answers1

0
With this code you will fetch all contact names from address book.I hope it will help you.


   -(void)addressbookfetch
    {   
      CFErrorRef error = NULL;
      ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
      CFArrayRef people=ABAddressBookCopyArrayOfAllPeople(addressBook);
       CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                                            kCFAllocatorDefault,
                                                             CFArrayGetCount(people),
                                                           people
                                                           );

       CFArraySortValues(
                  peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  (void*) ABPersonGetSortOrdering()
                  );
      if(addressBook != nil)
     {
        NSLog(@"Succesful.");
        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

        NSUInteger i = 0;
        for(i=0;i<[allContacts count];i++)
        {
            Person *person = [[Person alloc]init];

            ABRecordRef contactPerson = (__bridge  ABRecordRef)allContacts[i];


           ABMultiValueRef phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
           NSUInteger k=0;
           for(k=0;k<ABMultiValueGetCount(phoneNumber);k++)
           {
               NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
               NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

               NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName ?: @"",lastName ?: @""];

               fullName = [fullName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            [addressData addObject:fullName];
              NameStr = [[NSMutableString alloc] initWithString:fullName];
            NameStrnew=[NSString stringWithString:fullName];
               NSLog(@"Name string %@",NameStrnew);
              [phoneBookNames addObject:NameStrnew];



               NSString *phonenumber= (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, k);
               if(k==0)

              {
                  person.homePhone = phonenumber;
                  PhoneStr = [[NSMutableString alloc] initWithString:phonenumber];
                   PhoneStrnew =[NSString stringWithString:phonenumber];
                    NSLog(@"PhoneStrnew%@",PhoneStrnew);

                // [addressData addObject:phonenumber];
                   [phoneBookNumbers addObject:PhoneStrnew];

                }