Show The Contact List in Swift 3.0

The ContactUI framework provide the controllers for displaying, editing,  selecting and creating the contacts from your app.



This tutorial provide only to display contact from your contact apps. In ContactUI framework have a controller "CNContactPickerViewController", Which fetch all contact from your contact App. 

Follow the below steps for Display contact using  CNContactPickerViewController.

Step 1 : Import ContactUI framework in Swift file.
import ContactsUI
Step 2 : Add the CNContectPickerDelegate in Swift file.
class ViewController: UIViewController , CNContactPickerDelegate {
Step 3 : Add a Button, to present CNContactPickerViewController to display all concats.
@IBAction func click_Contact(_ sender: Any) {
    let cnPicker = CNContactPickerViewController()
    cnPicker.delegate = self
    self.present(cnPicker, animated: true, completion: nil)
}
Step 4 : The below method of CNContectPickerDelegate.
//MARK:- CNContactPickerDelegate Method

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    contacts.forEach { contact in
        for number in contact.phoneNumbers {
            let phoneNumber = number.value
            print("number is = \(phoneNumber)")
        }
    }
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
    print("Cancel Contact Picker")
}

See  Output :




Thanks.

Show The Contact List in Swift 3.0 Show The Contact List in Swift 3.0 Reviewed by KIRIT MODI on 01:48:00 Rating: 5

No comments:

Powered by Blogger.