gpt4 book ai didi

ios - CNContacts 未从联系人列表中获取所有联系人

转载 作者:行者123 更新时间:2023-11-30 11:26:27 24 4
gpt4 key购买 nike

CNContacts 未从联系人列表中获取所有联系人。一些联系方式丢失了。谁能帮忙。

这是我的代码。

    let key = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: key)
try! contactStore.enumerateContacts(with: request) { (contact, stoppingPointer) in
let givenName = contact.givenName
let familyName = contact.familyName
print(contact.phoneNumbers[0].label!)
var number = [contact.phoneNumbers[0].value.stringValue]
var type = [contact.phoneNumbers[0].label]
if contact.phoneNumbers.count > 1{
for i in 1...contact.phoneNumbers.count - 1{
number.append(contact.phoneNumbers[i].value.stringValue)
type.append(contact.phoneNumbers[i].label)
}
}
let imagedata = contact.imageData
let contactToAppend = ContactStruct(givenName: givenName, familyName: familyName, number: number, type: type as! [String] , image: imagedata)
self.contacts.append(contactToAppend)
}

最佳答案

请尝试下面的代码希望它能工作。

func getContactList() {

let contacts = self.getContactFromCNContact()

for contact in contacts {

//do your stuff with contact
}
}

func getContactFromCNContact() -> [CNContact] {

let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactIdentifierKey,
CNContactNamePrefixKey,
CNContactGivenNameKey,
CNContactMiddleNameKey,
CNContactFamilyNameKey,
CNContactPreviousFamilyNameKey,
CNContactNameSuffixKey,
CNContactNicknameKey,
CNContactOrganizationNameKey,
CNContactDepartmentNameKey,
CNContactJobTitleKey,
CNContactPhoneticGivenNameKey,
CNContactPhoneticMiddleNameKey,
CNContactPhoneticFamilyNameKey,
CNContactBirthdayKey,
CNContactNonGregorianBirthdayKey,
CNContactNoteKey,
CNContactImageDataKey,
CNContactThumbnailImageDataKey,
CNContactImageDataAvailableKey,
CNContactTypeKey,
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey,
CNContactPostalAddressesKey,
CNContactDatesKey,
CNContactUrlAddressesKey,
CNContactRelationsKey,
CNContactSocialProfilesKey,
CNContactInstantMessageAddressesKey] as [Any]

//Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containers(matching: nil)
} catch {
print("Error fetching containers")
}

var results: [CNContact] = []

// Iterate all containers and append their contacts to our results array
for container in allContainers {

let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)

do {
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
results.append(contentsOf: containerResults)

} catch {
print("Error fetching results for container")
}
}

return results
}

关于ios - CNContacts 未从联系人列表中获取所有联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com