1

I think I need to use ABMultiValueCopyArrayOfAllValues to grab all phone numbers from my ABAddressBookRef reference variable.

(If this is not the correct way to do it, please give me a correct way; ANY way that gives me access to contact phone numbers will do)

What I'd like to do now, is grab my user's contact's phone numbers(preferably their cell) and add that to an array. How do I grab just the numbers and add that to an array?

Any help, suggestions, or advice with this is greatly appreciated in advice, I'm not finding this anywhere.

Thanks.

h4x0rjax
  • 359
  • 1
  • 6
  • 15
  • 1
    Have you looked at the [Address Book Programming Guide for iOS](https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html#//apple_ref/doc/uid/TP40007744)? It has sample code for accessing the phone numbers of a contact. – rmaddy Jan 25 '14 at 22:27
  • I have not- looking now. – h4x0rjax Jan 25 '14 at 22:31

2 Answers2

1

If I were you, I would use this approach:

NSMutableArray *allPhoneNumbers = @[].mutableCopy;
NSArray *allContact = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(book);
for (id rec in allContacts){
    ABMultiValueRef mvr = ABRecordCopyValue((__bridge ABRecordRef)rec, kABPersonPhoneProperty);
    NSArray *currentNums = (__bridge NSArray*) ABMultiValueCopyArrayOfAllValues(mvr);
    [allPhoneNumbers addObjectsFromArray: currentNums];
}

I have not tested this, but it should work. Tell me if you have any issues.

If you want just one contact's numbers (the above gets every contact), use this code.

ABMultiValueRef mvr = ABRecordCopyValue(yourRecordRef, kABPersonPhoneProperty);
NSArray *currentNums = (__bridge NSArray*) ABMultiValueCopyArrayOfAllValues(mvr);
[allPhoneNumbers addObjectsFromArray: currentNums];
erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • Hey I tried your code. Had an error on `[allPhoneNumbers addObject: (__bridge NSString *) phone];`, error is **red error** saying: `incompatible types casting 'id' to 'NSString *' with a __bridge cast`, that's for the top code in the `for` – Chisx Jan 26 '14 at 01:14
  • also when you `addObject:` phone(same line that the error's on), what's the variable `phone`? shouldn't you add `currentNums` to `allPhoneNumbers`? – Chisx Jan 26 '14 at 01:23
  • @Chisx I fixed the error you pointed out (by changing it to a regular cast, not bridge). As to your second comment, `phone` is one of the strings in the multi value ref for your record. Because you bridge the original CFArrayRef to NSArray, it apparently changes the CFStringRefs to NSString conveniently. While it is not necessary, I will revise my code to be more direct – erdekhayser Jan 26 '14 at 02:54
  • @Chisx if it helped, a +1 would be appreciated – erdekhayser Jan 26 '14 at 14:15
  • sorry haven't been on in a minute, still going to test this out. Just for clarity, what is the `id` rec? more specifically what is `rec`, is that just an id variable creation? – Chisx Jan 26 '14 at 20:35
  • Your answer works. very nice. I'm printing `allPhoneNumbers` and getting some print-offs that look like this(I changed it a bit): "(615)\U00a0534-7081", do you happen to know what exactly is going on with the '\U00a0'? (There's multiple prints doing this) – Chisx Jan 26 '14 at 20:52
  • According to this answer (http://stackoverflow.com/a/20532491/2035473) to get rid of this strange \U00a0, you should use this line: `phoneNumber = [phoneNumber stringByReplacingOccurencesOfString:@"." withString:@""];` Put this in a for loop that iterates through the array of phone numbers and you should be good! – erdekhayser Jan 26 '14 at 21:59
  • 1
    @Chisx to understand the whole `for(id rec in myArray)`, look at this thread: http://stackoverflow.com/questions/992901/how-do-i-iterate-over-an-nsarray – erdekhayser Jan 26 '14 at 22:02
  • An enumeration idiom. Brilliant. @for i in range awesome – Chisx Jan 26 '14 at 22:08
0
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);    
for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++)    
{  
  CFStringRef phoneNumberRefCF = ABMultiValueCopyValueAtIndex(phoneNumbers, j);  
  CFStringRef locLabelCF = ABMultiValueCopyLabelAtIndex(phoneNumbers, j);  
  NSString *phoneLabelCF =(NSString*) ABAddressBookCopyLocalizedLabel(locLabelCF);  
  NSString *phoneNumberCF = (NSString *)phoneNumberRefCF;  
  CFRelease(phoneNumberRefCF);  
  CFRelease(locLabelCF);  
  NSLog(@"  - %@ (%@)", phoneNumberCF, phoneLabelCF);  
}
Prad
  • 51
  • 2