如何检索ios 9中的联系号码

Aru*_*n V 6 ios9 xcode7 swift2

在ios 8中,我使用以下代码打印用户的联系号码,

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
Run Code Online (Sandbox Code Playgroud)

println(phoneNumber)}}

但苹果在ios 9中引入了新的联系框架.现在我坚持检索联系号码.我在苹果网站和其他网站上找到了一些代码,如下所示,但仍然不是只打印联系人号码,

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)
                    }
                }
Run Code Online (Sandbox Code Playgroud)

小智 21

这应该做到这一点.

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

以(555)766-4823的风格打印