简体   繁体   中英

Fetching email contacts from Phonebook iOS Swift

I would want to retrieve email contacts from Phone book from iOS using Swift. This is how I'm doing:

override func viewDidLoad() {
    super.viewDidLoad()
    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self
    contactPicker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
    present(contactPicker, animated: true, completion: nil)
}
func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]) {

    var emailContact = contacts.map { $0.emailAddresses.first!.value as String }
    print(emailContact) //It's actually not returning anything.

}

Resultant screen is: Resultant Screen

What I would want is to appear it something like: Expected resultant

I imported Contacts and ContactsUI and also declared protocol CNContactPickerDelegate I really can't figure out why the didSelectContacts function is not working.

So I just figured out what was causing the issue. Now It's working fine for me, I was using didSelectContacts instead of didSelect contacts . This is letting me select multiple contacts

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    var emailContact = contacts.map { $0.emailAddresses.first!.value as String }
    print(emailContact) 
    //returns: ["John-Appleseed@mac.com", "kate-bell@mac.com"]
}

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