如何找回ios中的联系电话 9

How to retrieve the contact number in ios 9

在ios8中,我用下面的代码打印了用户的联系电话,

if let contacts = ABAddressBookCopyArrayOfAllPeople(self.addressBookRef)?.takeRetainedValue() as? NSArray {
            for record:ABRecordRef in contacts {

                let phones:ABMultiValueRef = ABRecordCopyValue(record, kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef
                for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
                {
                    let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
                    let phoneNumber : String = phoneUnmaganed.takeUnretainedValue() as! String

println(电话号码) } }

但是 apple 在 ios9 中引入了新的联系人框架。现在我一直在检索联系人号码。我在苹果网站和其他网站中找到了一些代码,如下所示,但它仍然不完全只打印联系电话,

contacts = try store.unifiedContactsMatchingPredicate(
                CNContact.predicateForContactsMatchingName("Siva"),
                keysToFetch:[CNContactPhoneNumbersKey])
            for contact:CNContact in contacts {
                if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
                    for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                        print(phoneNumber.value)
                    }
                }

这应该可以做到。

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

印刷风格 (555) 766-4823