-1

i am developing contact list app so that i need to bring the contact data and stored in the tableview i did as below

     - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
   {
     NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

for (int i = 0; i < numberOfPeople; i++)
{
    ABRecordRef person = (__bridge ABRecordRef)contactList[i];

   firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
 lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
    {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        NSLog(@"  phone is:%@", phoneNumber);
    }
         CFRelease(phoneNumbers);

           }


        }

aim getting the data in the strings format that is first name and the last name .my problem is i need to save the data that is present in first name and last name to array But that array is present out side of the for loop . so that i will pass the array data to the table view .

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

1.option 1 : Just do one thing create NSMutableDictionary inside for loop. Store all ( firstname,lastname etc) in it. at last of for loop add it in array. 2.option 2: create contact as object with required attributes. See example here https://stackoverflow.com/questions/24552545/how-to-get-phonebook-contacts-from-ios-device

Community
  • 1
  • 1
Sandeep
  • 766
  • 5
  • 16
0
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{

    NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    NSMutableArray * contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

    NSMutableArray *tableviewArray =[[NSMutableArray alloc] init]; // ADD This Line

    for (int i = 0; i < numberOfPeople; i++)
    {
        ABRecordRef person = (__bridge ABRecordRef)contactList[i];

        NSString *  firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *  lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);




        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
        {
            NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
            NSLog(@"  phone is:%@", phoneNumber);
        }

        [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , nil]]; // ADD This Line

        CFRelease(phoneNumbers);

    }


}
neo D1
  • 1,710
  • 13
  • 15
  • AS u have given tableview array is only for that loop if array comes out of that loop it returns null value i need to keep data same when ever it has been kept in different methods and what about phone numbers thanks in advance please help. – iOS devloper Oct 04 '14 at 05:35
  • Create a global array – neo D1 Oct 04 '14 at 05:36
  • kk i got but aim getting phone no for last index names only it means its showing phone number for las name only not for all please let me know – iOS devloper Oct 04 '14 at 05:44
  • NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, numberOfPhoneNumbers - 1)); [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , phoneNumber , @"phone_no", nil]]; // ADD This Line – neo D1 Oct 04 '14 at 05:47
  • kk thank u it got worked and can we able to filter the name &no by using the search bar can u give me any examples i have tried a lot but i can't able to find id – iOS devloper Oct 04 '14 at 05:54
  • http://www.ioscreator.com/tutorials/add-seachbar-to-tableview-ios7 See if this helps – neo D1 Oct 04 '14 at 06:16