0

My question is basically the same as the question here:

iOS Swift: Get user selected phone number from CNContactProperty as a string

WHICH HAS BEEN WRONGLY MARKED AS DUPLICATE WITHOUT SOLUTIONS (Reason stated in that post).

I desperately want to know the answer, it should be very simple, because I can see Skype is using exactly the same API to retrieve contacts and I want to know how.

Swift or Objective C is not important only the idea matters, thank you.

Community
  • 1
  • 1
steven
  • 1,237
  • 2
  • 11
  • 13

2 Answers2

2

After a few hours trying I figured it out myself:

We first need to get the "identifier" property of CNContactProperty and then fetch the chosen number that matching it.

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    CNContact *contact = contactProperty.contact;
    NSString *identify = contactProperty.identifier;//pick the number according to this id!!!
    _lastDisplay = @"";
    for (CNLabeledValue<CNPhoneNumber*>* number in contact.phoneNumbers) {
        if ([number.identifier isEqualToString:identify]) {
            _lastDisplay = ((CNPhoneNumber *)number.value).stringValue;
        }
    }
}

Leave it here if anyone needs it.

steven
  • 1,237
  • 2
  • 11
  • 13
-1

Swift

You can simply type cast CNContactProperty to CNPhoneNumber.

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
    if let phoneNumber = contactProperty.value as? CNPhoneNumber {
        print(phoneNumber.stringValue)
    }
}
Marián Černý
  • 15,096
  • 4
  • 70
  • 83