如何获取手机号码和家庭电话号码与iOS中的联系人不同

时间:2016-04-30 07:40:39

标签: ios cncontact cncontactstore

我已经提取了联系电话,但我只得到一个号码,即家庭号码或手机号码。如何取两者?有没有具体的方法呢? 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用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.
    }

}