简体   繁体   中英

How do i compile all contact's phone numbers into one cell - using CNContact

My code I am using to source the contact's phone number is below, what i would like to do is have all the contacts phone numbers (landline, mobiles etc) shown together, example 0402 000 000, 618 802336, 0423 000 000 (together in the one textField)

My code below

// Retrieve contact details ---------------------------
    @IBAction func show(_ sender: Any) {
        let contacVC = CNContactPickerViewController()
        contacVC.delegate = self
        self.present(contacVC, animated: true, completion: nil)
    }

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

        print(contact.phoneNumbers)
        let numbers = contact.phoneNumbers.first
        print((numbers?.value)?.stringValue ?? "")
        self.Phone.text = "\((numbers?.value)?.stringValue ?? "")"
            }

    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        self.dismiss(animated: true, completion: nil)
    }

You can display all phone number as like below

let contact = CNContact()
let numbers = contact.phoneNumbers.compactMap { $0.value.stringValue }
self.Phone.text = numbers.joined(separator: ", ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM