0

I have fetched the contact numbers but i get only one number, that is either home number or mobile number. how to fetch them both? is there any specific way to do it? any help will be appreciated.

Amrut Gaikwad
  • 59
  • 2
  • 11

1 Answers1

1

You can use CNContactPickerViewController.

import UIKit
import Contacts
import ContactsUI

protocol AddContactViewControllerDelegate {
    @available(iOS 9.0, *)
    func didFetchContacts(contacts: [CNContact])
}

class ViewController: UIViewController,CNContactPickerDelegate {

    var delegate: AddContactViewControllerDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()


        if #available(iOS 9.0, *) {
            let contactPickerViewController = CNContactPickerViewController()

            contactPickerViewController.predicateForSelectionOfContact = NSPredicate(format: "phoneNumbers.@count > 0", argumentArray: nil)

            contactPickerViewController.delegate = self

            presentViewController(contactPickerViewController, animated: true, completion: nil)
        } else {
            // Fallback on earlier versions
        }

    }

    // MARK: CNContactPickerDelegate function

    @available(iOS 9.0, *)
    func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
        print(contact.phoneNumbers)

        if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
            for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                let a = phoneNumber.value as! CNPhoneNumber
                print("\(a.stringValue)")
            }
        }

       // delegate.didFetchContacts([contact])
        navigationController?.popViewControllerAnimated(true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
Keyur Hirani
  • 1,607
  • 14
  • 22
  • can you send me the code for objective c – Amrut Gaikwad Apr 30 '16 at 08:04
  • any one please help me.. – Amrut Gaikwad Apr 30 '16 at 09:52
  • http://stackoverflow.com/questions/32802087/cnui-error-selection-predicates-are-set-but-the-delegate-does-not-implement-cont – Keyur Hirani Apr 30 '16 at 10:02
  • the contacts which are available in contacts i have to display them on table view, like if there are two numbers for one contact(office and home) i have display them both...if i have one number for another contact then i will display only one number and so on..how to achieve this?? any help will be appreciated. – Amrut Gaikwad Apr 30 '16 at 15:15