【问题标题】:How to retrieve phone number from Contacts framework如何从联系人框架中检索电话号码
【发布时间】:2016-09-26 10:42:27
【问题描述】:

尝试检索手机号码的新联系人。我有地址名称电子邮件,但无法弄清楚手机。这就是我得到的。标有** 的部分是我出错的地方。

if let oldContact = self.contactItem {
    let store = CNContactStore()

    do {
        let mykeysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactEmailAddressesKey, CNContactPostalAddressesKey,CNContactImageDataKey, CNContactImageDataAvailableKey,CNContactPhoneNumbersKey]
        let contact = try store.unifiedContactWithIdentifier(oldContact.identifier, keysToFetch: mykeysToFetch)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            if contact.imageDataAvailable {
                if let data = contact.imageData {
                    self.contactImage.image = UIImage(data: data)
                }
            }

            self.fullName.text = CNContactFormatter().stringFromContact(contact)
            self.email.text = contact.emailAddresses.first?.value as? String
            self.phoneNumber.text = contact.phoneNumbers.first?.value as? String

            **if contact.isKeyAvailable(CNContactPhoneNumbersKey){
                if let phoneNum = contact.phoneNumbers.first?.value as? String {
                    self.phoneNumber.text = phoneNum as String
                }

            }**

            if contact.isKeyAvailable(CNContactPostalAddressesKey) {
                if let postalAddress = contact.postalAddresses.first?.value as? CNPostalAddress {
                    self.address.text = CNPostalAddressFormatter().stringFromPostalAddress(postalAddress)
                } else {
                    self.address.text = "No Address"
                }
            }
        })
    } catch {
        print(error)
    }
}

【问题讨论】:

    标签: swift contacts


    【解决方案1】:

    如果您想要一个联系人的手机列表,请查看phoneNumbers,它是CNLabeledValue 的数组,并找到labelCNLabelPhoneNumberMobileCNLabelPhoneNumberiPhone 的那些。

    例如,您可以执行以下操作:

    let mobilePhoneLabels = Set<String>(arrayLiteral: CNLabelPhoneNumberMobile, CNLabelPhoneNumberiPhone, "cell", "mobile") // use whatever you want here; you might want to include a few strings like shown here to catch any common custom permutations user may have used
    
    let mobileNumbers = contact.phoneNumbers.filter { mobilePhoneLabels.contains($0.label) && $0.value is CNPhoneNumber }
        .map { ($0.value as! CNPhoneNumber).stringValue }
    

    所以如果你想要第一个:

    let mobileNumber = mobileNumbers.first ?? ""   // or use `if let` syntax
    

    或者如果你想要它们列表的字符串表示:

    let mobileNumberString = mobileNumbers.joinWithSeparator(" ; ")
    

    您如何处理这组手机号码取决于您自己,但希望这能说明基本概念。

    【讨论】:

    • label和CNLabeledValue有什么区别?
    • A CNLabeledValue 是一个由label 组成的对象(例如“家”、“工作”、“移动”等;这些通常使用CNLabelPhoneNumber... 常量编码为最大化本地化兼容性,尽管它们也可以由简单的字符串组成)和value(数字本身的CNPhoneNumber)。见developer.apple.com/library/ios/documentation/Contacts/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2011-03-05
    • 1970-01-01
    相关资源
    最近更新 更多