Swift – Import contacts from phonebook using Contacts framework

Get contacts from Phonebook in Swift

This Swift tutorial will show you how to work with Contacts framework and import contacts in your app. Contacts framework is working from iOS 9 and above. I have created a separate class to handle the contacts, so I will explain it here step by step.

 

1. We need to check if the user approved permissions for entering his Phonebook, and also import the framework.

import Contacts
import ContactsUI


class ContactsHandler: NSObject,CNContactPickerDelegate {
    static let sharedInstance = ContactsHandler()
    
    var contactStore = CNContactStore()
    var parentVC: UIViewController!

    
    func requestForAccess(completionHandler: (accessGranted: Bool) -> Void) {
        let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
        
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)
            
        case .Denied, .NotDetermined:
            self.contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
                if access {
                    completionHandler(accessGranted: access)
                }
                else {
                    if authorizationStatus == CNAuthorizationStatus.Denied {
                        let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
                        print(message);
                    }
                }
            })
            
        default:
            completionHandler(accessGranted: false)
        }
    }

2. Then we will proceed with presenting the native phonebook using CNContactsPickerController

    func openAddressbook(vc: UIViewController){
        requestForAccess { (accessGranted) in
            if accessGranted == true{
                self.parentVC = vc;
                let controller = CNContactPickerViewController()
                controller.delegate = self
                vc.presentViewController(controller,animated: true, completion: nil)
            }
        }
    }

3. Listen to the selected contact and the cancel button of the phonebook, by using these two delegate methods

    func contactPicker(picker: CNContactPickerViewController,
                       didSelectContact contact: CNContact) {
        
        if contact.isKeyAvailable(CNContactPhoneNumbersKey){
            // handle the selected contact
        }
    }
    
    func contactPickerDidCancel(picker: CNContactPickerViewController) {
        print("Cancelled picking a contact")
    }

4. If you want to use multiple selections then use this delegate method, but you will need to comment the above single selection method (didSelectContact):

    func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]){
        for contact in contacts{
            if contact.isKeyAvailable(CNContactPhoneNumbersKey){
                // handle contacts here
            }
        }
    }

That would be it from this detailed tutorial on how to use Contacts framework. If it helped you please don’t forget to share. 🙂

Recommended for you: How to convert HTML using NSAttributedString

Leave a Reply

Your email address will not be published. Required fields are marked *