Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用swift中的ContactsUI获取所有联系人电话号码_Ios_Swift_Contacts - Fatal编程技术网

Ios 使用swift中的ContactsUI获取所有联系人电话号码

Ios 使用swift中的ContactsUI获取所有联系人电话号码,ios,swift,contacts,Ios,Swift,Contacts,我使用ContactsUI获取所有联系人并在tableview中显示。在我的方法中,我只获取联系人的一个电话号码。我的问题是如何在我的方法中获取联系人的所有电话号码 我的模型和繁殖方法: import Foundation import UIKit import Contacts class ContactsModel : NSObject { let givenName:String let familyName:String let phoneNumber:Stri

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

我的模型和繁殖方法:

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{ $0.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帐户)的联系人,然后返回相关联系人。我们还可以使用此联系人框架搜索和筛选联系人

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")
    }
注意:不要忘记添加访问联系人的隐私说明 将信息导入info.plist


我需要导入库才能使用CNContactStore吗?我使用了未解析的标识符“CNContactStore”。您是否
导入联系人
?顺便问一下:我没有编写/进一步检查代码,我只是更正了其中的一个错误;)