1

I want to get the phone number for a selected contact from the address book and then call that number, yet for some contacts this is achieved, while for others the returned phone number is NULL! I checked in my phone to see differences between 2 contacts (for one of them, the code returns the correct number, and for the other, it returns NULL) and I can see absolutely no difference (both contacts have first name, last name, and mobile phone number). I'm interested in the reason why this is happening and also for a solution to get the phone no. of a contact when it's available. Thanks in advance!

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);
if (phoneNumbers[0] != NULL) {;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", phoneNumbers[0]]]];
}
[self dismissViewControllerAnimated:YES completion:nil];
return NO;

}

Gina
  • 89
  • 6

1 Answers1

0

I think I may know the problem but without looking at your sample data its just a hunch.

The reason you are getting null for one of your contacts is that for that user he may have 2 phone numbers listed on his address book . One phone number is blank/no value and the other one phone field does have a value (for the same contact).

Look at your code. Check what is the size of this array for each contact

NSArray* phoneNumbers;
[phoneNumbers count];

If this returns 2 for one of them then you know that user had two phone numbers in its array and you need to check

phoneNumbers[0]
phoneNumbers[1]
Sam B
  • 27,273
  • 15
  • 84
  • 121
  • I have tried this, but it still doesn't work: for some contacts which have 1/more phone numbers, the array contains 0 elements! Works perfectly with contacts generated in the simulator though... – Gina May 04 '14 at 19:34