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

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:39:46

问题


As the title suggests, in my iOS app using swift I've got a CNContactProperty object and I want to extract the phone number from it as a string.

The CNContact property is returned from the standard CNContactPickerViewController via the delegate protocol function, after the user has selected a contact from it.

When a contact has multiple phone numbers, I want to be able to extract the one that the user tapped on in the contact view from the CNContactProperty.

I'm trying to do something like this:

 let myString = theCNContactProperty.value as! String

However, this crashes with an (lldb) error. I suspect that maybe the "value" property is not what I need?

I'm able to retrieve arbitrary numbers like so:

let myString = contactProperty.contact.phoneNumbers[0].value.stringValue

Which returns the first number a contact has. However, this doesn't serve my purpose as I want to be able to extract the specific number selected by the user when a contact has more than 1 number.

I've been working on this for hours and can't figure it out, I'd appreciate any help you can give me!

Edit: This is NOT a duplicate of the provided link. The linked question is about retrieving all numbers from a contact, NOT a specifically selected one. There is a HUGE difference in that.


回答1:


So you said that you want to be able to get the phone number the user selected from the CNContactViewController.

The CNContactViewController has a delegate function that returns the key the user selected. This is the function:

optional func contactViewController(_ viewController: CNContactViewController, 
  shouldPerformDefaultActionFor property: CNContactProperty) -> Bool

In that function, you can get the selected phone number by doing this:

let myString = property.identifier

Also, if you return false in this function, the action wont take place which I think means it wont automatically call the number.




回答2:


You can use this - although, if the contact has more than one phone number, this will only get the first one...

var thePhoneLabel: String?
var thePhoneNumber: String?

func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {

    picker.dismissViewControllerAnimated(true, completion: {

        if contact.phoneNumbers.count > 0 {

            if let anEntry = contact.phoneNumbers.first {
                if let theNumber = anEntry.value as? CNPhoneNumber {

                    // Get the label for the phone number (Home, Work, Mobile, etc)
                    self.thePhoneLabel = CNLabeledValue.localizedStringForLabel(anEntry.label)

                    // Get the actual phone number (as a string)
                    self.thePhoneNumber = theNumber.stringValue

                }
            }

        } else {
            // contact has no phone numbers
            self.thePhoneLabel = "(No Phone)"
            self.thePhoneNumber = "(No Phone)"
        }

    })

}

EDIT:

If using:

contactPickerViewController.displayedPropertyKeys = [CNContactPhoneNumbersKey]

then:

var theContactName: String?
var thePhoneNumber: String?
var thePhoneLabel: String?

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {

    theContactName = contactProperty.contact.givenName
    thePhoneNumber = contactProperty.value?.stringValue

    if let lbl = contactProperty.label {
        thePhoneLabel = CNLabeledValue.localizedStringForLabel(lbl)
    }

}

Note:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)

function will only be called if you do NOT have a

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)



来源:https://stackoverflow.com/questions/42631312/ios-swift-get-user-selected-phone-number-from-cncontactproperty-as-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!