Swift 从联系人框架中获取联系人不起作用
在我的应用程序中,我创建了一个CNContact并将其保存到contacts store,同时还保存了联系人的标识符。在应用程序的另一部分中,我使用标识符获取该联系人,但在iOS 13中不起作用。(这在iOS 12上确实有效 注意:我知道iOS 13上有联系人备注键权限,但我没有获取该字段 以下是我创建联系人的方式:Swift 从联系人框架中获取联系人不起作用,swift,ios13,ios12,cncontact,cncontactstore,Swift,Ios13,Ios12,Cncontact,Cncontactstore,在我的应用程序中,我创建了一个CNContact并将其保存到contacts store,同时还保存了联系人的标识符。在应用程序的另一部分中,我使用标识符获取该联系人,但在iOS 13中不起作用。(这在iOS 12上确实有效 注意:我知道iOS 13上有联系人备注键权限,但我没有获取该字段 以下是我创建联系人的方式: func createPlaceholderContactFor(telephoneNumber: String) { guard placeholderC
func createPlaceholderContactFor(telephoneNumber: String) {
guard
placeholderContactIdentifier == nil,
CNContactStore.authorizationStatus(for: .contacts) == .authorized
else {
return
}
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
contact.imageData = UIImage(named: "MainIcon")?.jpegData(compressionQuality: 1.0)
contact.givenName = placeholderDefaultName
contact.familyName = ""
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:telephoneNumber))]
// Saving the newly created contact
let saveRequest = CNSaveRequest()
saveRequest.add(contact, toContainerWithIdentifier:nil)
do {
let store = CNContactStore()
try store.execute(saveRequest)
if contact.isKeyAvailable(CNContactIdentifierKey) {
print(contact.identifier) //this is printed on console and contact appears on phone app.
placeholderContactIdentifier = contact.identifier
}
} catch {
print(error.localizedDescription)
}
}
private func placeholderContact() -> CNContact? {
guard
let identifier = placeholderContactIdentifier,
CNContactStore.authorizationStatus(for: .contacts) == .authorized
else { return nil }
let predicate: NSPredicate = CNContact.predicateForContacts(withIdentifiers: [identifier])
let keysToFetch = [
CNContactIdentifierKey as CNKeyDescriptor,
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor
]
do {
let store = CNContactStore()
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
return contacts.first
}
catch {
print(error.localizedDescription)
return nil
}
}
这是我获取联系人的方式:
func createPlaceholderContactFor(telephoneNumber: String) {
guard
placeholderContactIdentifier == nil,
CNContactStore.authorizationStatus(for: .contacts) == .authorized
else {
return
}
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
contact.imageData = UIImage(named: "MainIcon")?.jpegData(compressionQuality: 1.0)
contact.givenName = placeholderDefaultName
contact.familyName = ""
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:telephoneNumber))]
// Saving the newly created contact
let saveRequest = CNSaveRequest()
saveRequest.add(contact, toContainerWithIdentifier:nil)
do {
let store = CNContactStore()
try store.execute(saveRequest)
if contact.isKeyAvailable(CNContactIdentifierKey) {
print(contact.identifier) //this is printed on console and contact appears on phone app.
placeholderContactIdentifier = contact.identifier
}
} catch {
print(error.localizedDescription)
}
}
private func placeholderContact() -> CNContact? {
guard
let identifier = placeholderContactIdentifier,
CNContactStore.authorizationStatus(for: .contacts) == .authorized
else { return nil }
let predicate: NSPredicate = CNContact.predicateForContacts(withIdentifiers: [identifier])
let keysToFetch = [
CNContactIdentifierKey as CNKeyDescriptor,
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor
]
do {
let store = CNContactStore()
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
return contacts.first
}
catch {
print(error.localizedDescription)
return nil
}
}
我还尝试使用
let contact=try store.unifiedContact(with identifier:identifier,keystefetch:keystefetch)
而不是let contacts=try store.unifiedContacts(matching:predicate,keystefetch:keystefetch)
但这会引发一个错误。打印(error.localizedDescription)调用的输出是什么?如果在调试器中设置了异常断点,代码将在何处停止?@GeneZ.Ragan当我调用store.unifiedContact(带标识符:keystefech:)
错误是:更新的记录不存在
打印(error.localizedDescription)调用的输出是什么?如果在调试器中设置了异常断点,代码在哪里停止?@GeneZ.Ragan当我调用store.unifiedContact(带标识符:keystefech:)
错误是:更新的记录不存在