Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode8 Swift3 - Problems with open contact list and retrieve data by click

Currently I'm developing my first iOS App and I'm a little slow and rude about the code (it's so weird and different from java) and, if this was the only problem, with the new update, Xcode is making my code insane. I think I solved most of the issues but...

Before, on one of the screens, the app opened a the address book and let the user click on one; when the clicked was done, the contact list close and data from that contact was retrieved to the controller. Now, if the user click on a contact, more info is displayed but any information come out of the console log.

I try everything I find on net and I'm not sure why is not working.

Before, I use Addressbook (or something like that) but I already tried with CNContact.

This is the Button code

@IBAction func addNewContactOnClick(_ sender: AnyObject) {

        let peoplePicker = CNContactPickerViewController()

        peoplePicker.delegate = self

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

CNContactPickerDelegate methods

func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]){
        contacts.forEach { contact in
            for number in contact.phoneNumbers {
                let phoneNumber = number.value as! CNPhoneNumber
                print("number is = \(phoneNumber)")
            }
        }
    }

func contactPickerDidCancel(picker: CNContactPickerViewController) {
    print("Cancel Contact Picker")
}
like image 964
Izahal Avatar asked Oct 06 '16 10:10

Izahal


Video Answer


1 Answers

Methods of CNContactPickerDelegate is changed in Swift 3 like below.

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    //your code
}

func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
    //your code
}

For other methods of CNContactPickerDelegate check Apple Documentation.

like image 181
Nirav D Avatar answered Oct 04 '22 21:10

Nirav D