0

我已经获取了联系号码,但我只得到一个号码,即家庭号码或手机号码。如何获取它们?有什么具体的方法吗?任何帮助将不胜感激。

4

1 回答 1

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.
    }

}
于 2016-04-30T07:54:38.440 回答