0

I am using the following code to get mobile number (i.e. 1st phone number) of all contacts in my address book. I want to store all those number into NSArray

self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);

for(int i=0; i < nPeople; i++ ){
    ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
    NSString *name = @"";

    if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
        name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [_dataSource addObject: name];

    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
    _phoneNumbers = (__bridge NSArray*)ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
    CFRelease(phoneNumberProperty);
    NSLog(@"Phone numbers = %@", _phoneNumbers);
}

but when I use NSLog(@"Phone numbers = %@", _phoneNumbers); the output comes out as

2012-11-07 15:31:06.116 contacts[4938:12b03] Phone numbers = 1 (800) 111-1111
2012-11-07 15:31:06.117 contacts[4938:12b03] Phone numbers = (222) 355-5668
2012-11-07 15:31:06.118 contacts[4938:12b03] Phone numbers = (910) 192-0192

I want the output in NSArray like

Phone numbers = ( 
"1 (800) 111-1111",
"(222) 355-5668",
"(910) 192-0192" 
)

how can I do this ?

Ankur Arya
  • 4,693
  • 5
  • 29
  • 50

2 Answers2

2

you Can Do it Simply like this

 self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    int nPeople = ABAddressBookGetPersonCount(addressBook);

    NSMutableArray *arrContacts = [[NSMutableArray alloc] init];

    for(int i=0; i < nPeople; i++ ){
        ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
        NSString *name = @"";

        if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
            name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        [_dataSource addObject: name];

        ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
        _phoneNumbers = (__bridge NSArray*)ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
        CFRelease(phoneNumberProperty);
        NSLog(@"Phone numbers = %@", _phoneNumbers);
        [arrContacts addObject:_phoneNumbers];
    }
    NSLog(@"Phone numbers : %@",arrContacts);
Sarafaraz Babi
  • 480
  • 6
  • 18
1

I found this via Google and was using this address book code as an example, but found a mistake that I'd correct for others coming here.

When using ABMultiValueCopyValueAtIndex it returns a type CFTypeRef. That function gets just the phone number at that index (in this case at index 0) which in the case of the phone number is a CFStringRef that shouldn't be cast to an NSArray*.

ABMultiValueCopyArrayOfAllValues would be the proper call to get the array of all phone numbers stored in a contact. That can be stored into an array and logged like Sarafaraz is doing in his answer.

NSArray *_phoneNumbers = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);

If you just want to get the first phone number you would do this:

CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
NSString *phoneString = (__bridge NSString*)phoneNumberRef;
teradyl
  • 2,584
  • 1
  • 25
  • 34