gpt4 book ai didi

swift - 如何获取新创建的联系人的contact.identifier

转载 作者:行者123 更新时间:2023-11-30 11:07:33 36 4
gpt4 key购买 nike

func createContact() {

let store = CNContactStore()
let contactToAdd = CNMutableContact()
contactToAdd.phoneticGivenName = "Harshad"
contactToAdd.phoneticMiddleName = "I"
contactToAdd.phoneticFamilyName = "Pipaliya"

contactToAdd.emailAddresses.append(CNLabeledValue(label: CNLabelPhoneNumberMain, value: "test@icloud.com"))

let saveRequest = CNSaveRequest()
saveRequest.add(contactToAdd, toContainerWithIdentifier: nil)
do {
try store.execute(saveRequest) // Here I need to get identifier of created new contact
}
catch {
print(error)
}
}

我尝试从应用程序创建标识符,但无法做到。

提前致谢

最佳答案

这是创建联系人时获取联系人id的方式

func createContact() {

let store = CNContactStore()
let contactToAdd = CNMutableContact()
contactToAdd.phoneticGivenName = "Harshad"
contactToAdd.phoneticMiddleName = "I"
contactToAdd.phoneticFamilyName = "Pipaliya"

contactToAdd.emailAddresses.append(CNLabeledValue(label: CNLabelPhoneNumberMain, value: "test@icloud.com"))

let saveRequest = CNSaveRequest()
saveRequest.add(contactToAdd, toContainerWithIdentifier: nil)
do {
try store.execute(saveRequest)
if contactToAdd.isKeyAvailable(CNContactIdentifierKey) {
print(contactToAdd.identifier) // this is the wat to get identifier of contact when creating new
}
}
catch {
print(error)
}
}

关于swift - 如何获取新创建的联系人的contact.identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52536573/

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