2

在花了很多时间进行故障排除、搜索并尝试了 StackOverFlow 上的一些答案之后。我无法让它正常工作,我需要能够选择联系人拥有的任何号码,但我总是得到CNLabelPhoneNumberMain或在某些情况下CNLabelPhoneNumberMobile即使选择家庭/工作号码。

长期以来,我一直面临着这个麻烦,如果有人能指出我的错误,我将不胜感激:

这是我的代码:

@IBAction func addSupporterContactTapped(_ sender: UIButton) {

     let contactPicker = CNContactPickerViewController()
     contactPicker.displayedPropertyKeys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
     contactPicker.delegate = self
     self.present(contactPicker, animated: true, completion: nil)
}

这就是选择的方式:

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

    let numbers = contactProperty.contact.phoneNumbers
    var phoneNumber: String?

            for contact in numbers {
        switch contact.label {
        case CNLabelHome:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelWork:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelOther:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberMain:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberiPhone:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberMobile:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberPager:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberHomeFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberWorkFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberOtherFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        default:
            break
        }
    }

但无论如何,正如我上面提到的,我从来没有得到正确选择的数字。

If it can help, When any number is selected, I get this printed in the console:

contact.label = _$!<Home>!$_
contact.label = _$!<Mobile>!$_
contact.label = _$!<HomeFAX>!$_

请问有人可以帮忙吗?

4

1 回答 1

2

当您在中选择一个电话号码时,您picker: didSelect contactProperty:CNPhoneNumbervaluecontactProperty

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
    if let phoneNumberObject = contactProperty.value as? CNPhoneNumber {
        let phoneNumber = phoneNumberObject.stringValue
        print(contactProperty.label!, phoneNumber)
    }
}
于 2019-12-09T10:11:11.923 回答