在 swift 中使用 ContactsUI 获取所有联系人 phone 号码

get all contact phone numbers with ContactsUI in swift

我使用 ContactsUI 获取所有联系人并在 tableview 中显示它们。在我的方法中,我只获取了一个 phone 个联系人。我的问题是如何在我的方法中获取所有 phone 个联系人号码。

我的模型和获取方法:

import Foundation
import UIKit
import Contacts

class ContactsModel : NSObject {
    let givenName:String
    let familyName:String
    let phoneNumber:String
    let emailAddress:String
    var identifier: String
    var image:UIImage

    init(givenName:String,familyName:String,phoneNumber:String,emailAddress:String,identifier:String,image:UIImage) {
        self.givenName = givenName
        self.familyName = familyName
        self.phoneNumber = phoneNumber
        self.emailAddress = emailAddress
        self.identifier = identifier
        self.image = image
    }

    class func generateModelArray() -> [ContactsModel]{
        let contactStore = CNContactStore()
        var contactsData = [ContactsModel]()
        let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey] as [CNKeyDescriptor]
        let request = CNContactFetchRequest(keysToFetch: key)
        try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
            let givenName = contact.givenName
            let familyName = contact.familyName
            let emailAddress = contact.emailAddresses.first?.value ?? ""
            let phoneNumber = contact.phoneNumbers.first?.value.stringValue ?? ""
            let identifier = contact.identifier
            var image = UIImage()
            if contact.thumbnailImageData != nil{
                image = UIImage(data: contact.thumbnailImageData!)!

            }else if  contact.thumbnailImageData == nil ,givenName.isEmpty || familyName.isEmpty{
                image = UIImage(named: "usertwo")!
            }
            contactsData.append(ContactsModel(givenName: givenName, familyName: familyName, phoneNumber: phoneNumber as String, emailAddress: emailAddress as String, identifier: identifier, image: image))
        })
        return contactsData
    }
}

不要将其作为字符串,而是使用字符串数组。

class ContactsModel : NSObject {
    let givenName: String
    let familyName: String
    let phoneNumber: [String]
    let emailAddress: String
    var identifier: String
    var image: UIImage

    init(givenName:String, familyName:String, phoneNumber:[String], emailAddress:String, identifier:String, image:UIImage) {
        self.givenName = givenName
        self.familyName = familyName
        self.phoneNumber = phoneNumber
        self.emailAddress = emailAddress
        self.identifier = identifier
        self.image = image
    }

    class func generateModelArray() -> [ContactsModel]{
        let contactStore = CNContactStore()
        var contactsData = [ContactsModel]()
        let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey] as [CNKeyDescriptor]
        let request = CNContactFetchRequest(keysToFetch: key)
        try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
            let givenName = contact.givenName
            let familyName = contact.familyName
            let emailAddress = contact.emailAddresses.first?.value ?? ""
            let phoneNumber: [String] = contact.phoneNumbers.map{ [=10=].value.stringValue }
            let identifier = contact.identifier
            var image = UIImage()
            if contact.thumbnailImageData != nil{
                image = UIImage(data: contact.thumbnailImageData!)!

            }else if  contact.thumbnailImageData == nil ,givenName.isEmpty || familyName.isEmpty{
                image = UIImage(named: "usertwo")!
            }
            contactsData.append(ContactsModel(givenName: givenName, familyName: familyName, phoneNumber: phoneNumber, emailAddress: emailAddress as String, identifier: identifier, image: image))
        })
        return contactsData
    }
}

iOS 9 开始,您可以访问 contact framework 的联系人。

Contacts Framework 不仅提供您 iphone 或 ipad 设备的数据库中的联系人,还提供来自 iCloud 帐户等各种来源的联系人,然后 return 关联的联系人。我们还可以使用此联系人框架搜索和过滤联系人。

1) 创建 CNContactStore 实例

let store = CNContactStore()

2) 在用户联系信息中添加您需要的密钥

let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey,CNContactNicknameKey]

3) 执行查询以获取所有用户名

   do {
        try store.enumerateContacts(with: CNContactFetchRequest.init(keysToFetch: keysToFetch as [CNKeyDescriptor]), usingBlock: { (contact, pointer) -> Void in
            print("contact = ","\(contact)")
            // Here Append this contact into your ViewModel Array.
        })
    } catch {
        print("something wrong happened")
    }

Note: Don't forget to add privacy description for accessing contacts information into info.plist