下面是我的代码。
let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactGivenNameKey,
CNContactMiddleNameKey,
CNContactFamilyNameKey,
CNContactPhoneNumbersKey
] as [Any]
//Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containers(matching: nil)
} catch let errorToShow{
//Handling error
}
我的代码适用于12.4.1以下版本,并且在iOS 13中为所有beta版本。现在,我正在iPhone XR中对其进行测试。
我正在获取联系人并使用我的自定义UI进行显示。我面临一个奇怪的问题,CNContactStore类没有给我联系人,它返回一个空数组。下面是我的代码。让...
if let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),CNContactGivenNameKey, CNContactMiddleNameKey,
CNContactFamilyNameKey,CNContactPhoneNumbersKey] as? [CNKeyDescriptor]{
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
do {
try contactStore.enumerateContacts(with: request){
(cont, stop) in
// Array containing all unified contacts from everywhere
}
} catch let errorToShow{
}
}