【问题标题】:How to retrieve the contact number in ios 9如何在 ios 9 中检索联系人号码
【发布时间】:2015-09-21 12:12:16
【问题描述】:

在 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

println(电话号码) } }

但苹果在 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)
                    }
                }

【问题讨论】:

  • 请帮我找回所有的联系电话,

标签: swift2 ios9 xcode7


【解决方案1】:

应该这样做。

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

以以下样式打印 (555) 766-4823

【讨论】:

  • store.unifiedContactsMatchingPredicate( CNContact.predicateForContactsMatchingName("Siva") ,我只想获取所有联系人。但这行代码帮助我们仅获取 Siva 的联系方式。您有什么建议吗? ? 提前致谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多