Contacts Framework in iOS

Apple provides ContactsUI framework for displaying and selecting the contacts. It fetches all the contacts from the iPhone's Contact App. Let's fire up the Xcode and create the project.


We're going to create a simple app for showing contact list and selecting the contacts then display it in label. ContactUI framework have a controller called CNContactPickerViewController which shows the contact lists.

Steps

  • Open ViewController.swift file and import ContactsUI
  • Add CNContactPickerDelegate in ViewController
  • Drag and drop the button, label in the View from Object Library for displaying contact
  • Then create an @IBOutlet variable for label and @IBAction function for button in ViewController.

import UIKit
import ContactsUI

class ViewController: UIViewController, CNContactPickerDelegate {
    
    @IBOutlet weak var numberLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
}

  • Then add the below code for displaying and cancelling the contact view controller. 

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        
        contacts.forEach { contact in
            for data in contact.phoneNumbers {
                let phoneNumber = data.value
                print("Your phone number is \(phoneNumber)")
                numberLabel.text = phoneNumber.stringValue
            }
        }
    }
    
    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        
        print("Cancelled the contact picker view controller")
    }

  • contactPicker(_, didSelect) execute when you're selecting the contacts from the contact view controller. After clicking the done button, you can easily retrieve the contacts from contacts array, then we're assigning the contact number for Label
  • contactPickerDidCancel(_ ) function is called when you're clicking the cancel button in contact view controller.

Full Code :

//
//  ViewController.swift
//  Contact List
//
//  Created by Vinoth Vino on 09/12/17.
//  Copyright © 2017 Vinoth Vino. All rights reserved.
//

import UIKit
import ContactsUI

class ViewController: UIViewController, CNContactPickerDelegate {
    
    @IBOutlet weak var numberLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @IBAction func showContacts(_ sender: Any) {
        
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        present(contactPicker, animated: true, completion: nil)
    }
    
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        
        contacts.forEach { contact in
            for data in contact.phoneNumbers {
                let phoneNumber = data.value
                print("Your phone number is \(phoneNumber)")
                numberLabel.text = phoneNumber.stringValue
            }
        }
    }
    
    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        
        print("Cancelled the contact picker view controller")
    }
}


Download:https://github.com/vinothvino42/CoderEarthExampleCodes.git

Output :









I hope you guys now understand how to fetch contacts in iOS. See you in the next tutorial..

Happy Coding 💻








Vinoth Vino