简体   繁体   中英

How do I display contacts when UITextField is clicked?

I have tried multiple times to create a UITextField that when clicked should show the contacts available on the device and retrieve the phone number and display it in the textfield. However I have been unable to do that. The best that I could do is to use a button to receive and display the number on a textfield. This works! How do I do the same for when the UITextField is clicked?

I'm running it on Xcode 10

private let contactPicker = CNContactPickerViewController()
    override func viewDidLoad() {
        super.viewDidLoad()
        configureTextFields()
        configureTapGesture()
        phonenumber.textContentType = .telephoneNumber

     }


    private func configureTextFields() {
        phonenumber.delegate = self

    }

    private func configureTapGesture(){
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap))
        viewcontact.addGestureRecognizer(tapGesture)

    }

    @objc private func handleTap(){
        viewcontact.endEditing(true)

    }



    @IBAction func pbbbbb(_ sender: Any) {
        contactPicker.delegate = self
        self.present(contactPicker, animated: true, completion: nil)


    }

}
    extension SelfTestTimer: CNContactPickerDelegate {

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


            let phoneNumberCount = contact.phoneNumbers.count

            guard phoneNumberCount > 0 else {
                dismiss(animated: true)

                return
            }

            if phoneNumberCount == 1 {
                setNumberFromContact(contactNumber: contact.phoneNumbers[0].value.stringValue)

            }else{

                let alertController = UIAlertController(title: "Select one of the numbers", message: nil, preferredStyle: .alert)

                for i in 0...phoneNumberCount-1 {
                    let phoneAction = UIAlertAction(title: contact.phoneNumbers[i].value.stringValue, style: .default, handler: {
                        alert -> Void in
                        self.setNumberFromContact(contactNumber: contact.phoneNumbers[i].value.stringValue)
                    })
                    alertController.addAction(phoneAction)
                }
                let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
                    alert -> Void in

                })
                alertController.addAction(cancelAction)

                dismiss(animated: true)
                self.present(alertController, animated: true, completion: nil)
            }
        }

        func setNumberFromContact(contactNumber: String) {


            var contactNumber = contactNumber.replacingOccurrences(of: "-", with: "")
            contactNumber = contactNumber.replacingOccurrences(of: "(", with: "")
            contactNumber = contactNumber.replacingOccurrences(of: ")", with: "")
            guard contactNumber.count >= 10 else {
                dismiss(animated: true) {
                    self.presentAlert(alertTitle: "", alertMessage: "A maximum of 10 contacts allowed per session", lastAction: nil)
                }
                return
            }
            phonenumber.text = String(contactNumber.suffix(10))

        }

        func contactPickerDidCancel(_ picker: CNContactPickerViewController) {

        }




}

extension SelfTestTimer: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


}

I want it so that when the UITextField is clicked, the contacts will appear and when one contact is selected, the number should appear in the textfield

You should use textFieldShouldBeginEditing method. Open the contacts controller in this method and return false , no need to add a gesture recogniser.

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