我很想知道这个问题,我花了很多力气才找到解决方案,但失败了。
我的问题是使用以下代码在电话簿中保存联系人详细信息和社交资料
func addProfileContact(completion: @escaping ()->Void) {
let contact = CNMutableContact()
addContact(Contact: populateProfileToContact(contact)) { (result) in
switch result{
case .Success(response: let bool):
if bool{
completion()
}
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
}
private func populateProfileToContact(_ contact: CNMutableContact) -> CNMutableContact {
if contact.imageData == nil{
contact.imageData = photoData
}
if let firstName = profile?.firstName {
contact.givenName = firstName
}
if let lastName = profile?.lastName {
contact.familyName = lastName
}
if let phone = profile?.phone {
contact.phoneNumbers = [CNLabeledValue(label: CNLabelWork, value:CNPhoneNumber(stringValue: phone))]
}
if let email = profile?.email! as NSString? {
contact.emailAddresses = [CNLabeledValue(label: CNLabelWork, value:email)]
}
contact.socialProfiles = []
if let networks = profile?.networks {
for network in networks {
if let socialNetwork = NetworkUtility.networkWith(slug: network.slug), let url = socialNetwork.url(for: network) {
//here i have used static values instead of original
contact.socialProfiles.append(CNLabeledValue(label: "DisplayName", value: CNSocialProfile(urlString: "UrlValue", username: "Username", userIdentifier: "Provider id", service: "displayName")))
}
}
}
return contact
}
问题是,执行此操作后,电话簿中的结果低于结果。 回到应用程序后,它与现有联系人不匹配:(