How to get the selected contact from CNContactPicker

北城余情 提交于 2019-12-11 18:16:32

问题


I'm currently able to get a contact from the contacts app, but the problem I'm facing that I need to be able to select the contact I want to import to my app , if the contact have more than 1 phone number, I always get the first number, here is the code I'm using:

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

    let numbers     = contactProperty.contact.phoneNumbers.first
    let firstName   = contactProperty.contact.givenName
    let lastName    = contactProperty.contact.familyName
    let phoneNumber = (numbers?.value)?.stringValue ?? ""

    /// Duplicate phone numbers will not be saved
    if phoneNumbers.contains(phoneNumber) {
        return
    }
    /// Saving selected contact in Core Data
    CoreDataManager.sharedInstance.savePhoneNumberInCoreData(FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber)

    DispatchQueue.main.async { [weak self] in
        self?.tableView.reloadData()
    }
}

The problem with line:

contactProperty.contact.phoneNumbers.first

There are two options only for contactProperty.contact.phoneNumbers .first or .last

If there is something like .selected, it would solve the problem.


回答1:


There is something called Main telephone number that you could use

var phoneNumber: String?

if let mainNumber = numbers.first(where: { $0.label == CNLabelPhoneNumberMain }) {
    phoneNumber = mainNumber.value.stringValue
} else {
    phoneNumber = numbers.first?.value.stringValue //or some other default value
}

Note that I changed the definition of numbers to be the array of phone numbers

let numbers = contactProperty.contact.phoneNumbers

Full code:

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

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

    if let mainNumber = numbers.first(where: { $0.label == CNLabelPhoneNumberMain }) {
        phoneNumber = mainNumber.value.stringValue
    } else {
        phoneNumber = numbers.first?.value.stringValue //or some other default value
    }

    if phoneNumber == nil || phoneNumbers.contains(phoneNumber) {
        return
    }

    let firstName = contactProperty.contact.givenName
    let lastName = contactProperty.contact.familyName   

    CoreDataManager.sharedInstance.savePhoneNumberInCoreData(FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber)

    DispatchQueue.main.async { [weak self] in
        self?.tableView.reloadData()
    }
}



回答2:


I'm agree with solution of Joakim Danielson.

But there are one more solution to get specific phone number which is stored in mobile number like home, mobile, fax etc.

Get all numbers from contact and enumerate on every number and check labeled values. See following code.

let numbers = contact.phoneNumbers

numbers.forEach { (c) in

    if let label = c.label {

        let localizedLabel = CNLabeledValue<NSCopying & NSSecureCoding>.localizedString(forLabel: label)
        print("\(localizedLabel)")

        switch localizedLabel.lowercased() {

        case "home":
            let homeNumber = c.value
            break

        case "mobile":
            let mobileNumber = c.value
            break

        default:
            break
        }
    }
}


来源:https://stackoverflow.com/questions/57769628/how-to-get-the-selected-contact-from-cncontactpicker

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