我正在尝试使用CNContactVCardSerialization
将联系人另存为vcf,效果比较好。我确实发现苹果不将Notes或Images包含在VCF中。我确实使用了stackoverflow answer来帮助图像序列化,但是现在我也在努力添加注释。
这是我尝试过的:
extension CNContactVCardSerialization {
internal class func vcardDataAppendingPhoto(vcard: Data, photoAsBase64String photo: String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardPhoto = "PHOTO;TYPE=JPEG;ENCODING=BASE64:".appending(photo)
let vcardPhotoThenEnd = vcardPhoto.appending("\nEND:VCARD")
if let vcardPhotoAppended = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: vcardPhotoThenEnd) {
return vcardPhotoAppended.data(using: .utf8)
}
return nil
}
internal class func vcardDataAppendingNote(vcard: Data, note:String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardNote = "NOTE:".appending(note)
let vcardNoteThenEnd = vcardNote.appending("\nEND:VCARD")
if let vcardNoteAppended = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: vcardNoteThenEnd) {
return vcardNoteAppended.data(using: .utf8)
}
return nil
}
class func data(jpegPhotoContacts: [CNContact]) throws -> Data {
var overallData = Data()
for contact in jpegPhotoContacts {
let data = try CNContactVCardSerialization.data(with: [contact])
if (contact.note != "") || contact.imageDataAvailable {
if contact.imageDataAvailable {
if let base64imageString = contact.thumbnailImageData?.base64EncodedString(),
let updatedData = vcardDataAppendingPhoto(vcard: data, photoAsBase64String: base64imageString) {
overallData.append(updatedData)
}
}
if contact.note != ""{
if let updatedData = vcardDataAppendingNote(vcard: data, note: contact.note){
overallData.append(updatedData)
}
}
} else {
overallData.append(data)
}
}
return overallData
}
}
这似乎不起作用。我可以使注释或图像部分正常工作,但不能同时工作。
我可能没有正确添加到VCF文件中。
非常感谢您的帮助。
答案 0 :(得分:0)
虽然不漂亮,但是这是我已经做过的事情,并且现在可以正常工作:
extension CNContactVCardSerialization {
internal class func vcardDataAppendingPhoto(vcard: Data, photoAsBase64String photo: String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardPhoto = "PHOTO;TYPE=JPEG;ENCODING=BASE64:".appending(photo)
if let vcardPhotoAppended = vcardAsString?.appending(vcardPhoto) {
return vcardPhotoAppended.data(using: .utf8)
}
return nil
}
internal class func vcardDataAppendingNote(vcard: Data, note:String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardNote = "NOTE:".appending(note)
let vcardFinal = vcardNote.appending("\n")
if let vcardNoteAppended = vcardAsString?.appending(vcardFinal) {
return vcardNoteAppended.data(using: .utf8)
}
return nil
}
internal class func vcardDataAppendingEnd(vcard: Data) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardEnd = "\nEND:VCARD"
if let vcardEndAppended = vcardAsString?.appending(vcardEnd) {
return vcardEndAppended.data(using: .utf8)
}
return nil
}
internal class func vcardDataCleanEnd(vcard: Data) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
if let vcardCleaned = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: "\n"){
return vcardCleaned.data(using: .utf8)
}
return nil
}
class func data(jpegPhotoContacts: [CNContact]) throws -> Data {
var overallData = Data()
for contact in jpegPhotoContacts {
let data = try CNContactVCardSerialization.data(with: [contact])
if (contact.note != "") || contact.imageDataAvailable {
if let updatedData = vcardDataCleanEnd(vcard: data){
overallData = updatedData
}
if contact.note != ""{
if let updatedData = vcardDataAppendingNote(vcard: overallData, note: contact.note){
overallData = updatedData
}
}
if contact.imageDataAvailable {
if let base64imageString = contact.thumbnailImageData?.base64EncodedString(),
let updatedData = vcardDataAppendingPhoto(vcard: overallData, photoAsBase64String: base64imageString) {
overallData = updatedData
}
}
if let updatedData = vcardDataAppendingEnd(vcard: overallData){
overallData = updatedData
}
} else {
overallData.append(data)
}
}
return overallData
}
}