Commit e27e7e7c authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

vCard: remove extra spaces in name

When saving vCard extra spaces was added in the name and first name
fields. This patch fix it by recreating vCard string.

Change-Id: I1b6d936348adb80d704461bab029c012cf37330d
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 8b4e9412
...@@ -30,26 +30,27 @@ enum VCardFields: String { ...@@ -30,26 +30,27 @@ enum VCardFields: String {
case photoJPEG = "PHOTO;TYPE=JPEG;ENCODING=BASE64:" case photoJPEG = "PHOTO;TYPE=JPEG;ENCODING=BASE64:"
case photoPNG = "PHOTO;TYPE=PNG;ENCODING=BASE64:" case photoPNG = "PHOTO;TYPE=PNG;ENCODING=BASE64:"
case end = "END:VCARD" case end = "END:VCARD"
case name = "N:"
case fullName = "FN:"
} }
extension CNContactVCardSerialization { extension CNContactVCardSerialization {
class func dataWithImageAndUUID(from contact: CNContact, andImageCompression compressedSize: Int?) throws -> Data { class func dataWithImageAndUUID(from contact: CNContact, andImageCompression compressedSize: Int?) throws -> Data {
var vcData = try CNContactVCardSerialization.data(with: [contact]) // recreate vCard string
let beginString = VCardFields.begin.rawValue + "\n"
let entryUIDString = VCardFields.uid.rawValue + contact.identifier + "\n"
let name = contact.familyName.trimmingCharacters(in: .whitespacesAndNewlines)
let firstnameString = VCardFields.name.rawValue + name + "\n"
let fullNameString = VCardFields.fullName.rawValue + name + "\n"
let endString = VCardFields.end.rawValue
guard var vcString = String(data: vcData, encoding: String.Encoding.utf8) else { var vCardString = beginString + entryUIDString + firstnameString + fullNameString + endString
return vcData
}
let entryUID = VCardFields.uid.rawValue + contact.identifier
vcString = vcString.replacingOccurrences(of: VCardFields.begin.rawValue,
with: (VCardFields.begin.rawValue +
"\n" + entryUID))
// if contact have an image add it to vCard data
guard var image = contact.imageData else { guard var image = contact.imageData else {
vcData = vcString.data(using: .utf8)! return vCardString.data(using: .utf8)!
return vcData
} }
var photofieldName = VCardFields.photoPNG var photofieldName = VCardFields.photoPNG
...@@ -57,7 +58,8 @@ extension CNContactVCardSerialization { ...@@ -57,7 +58,8 @@ extension CNContactVCardSerialization {
// if we need smallest image first scale it and than compress // if we need smallest image first scale it and than compress
var scaledImage: UIImage? var scaledImage: UIImage?
if compressedSize != nil { if compressedSize != nil {
scaledImage = UIImage(data: image)?.convert(toSize: CGSize(width:50.0, height:50.0), scale: 1) scaledImage = UIImage(data: image)?
.convert(toSize: CGSize(width: 50.0, height: 50.0), scale: 1)
} }
if let scaledImage = scaledImage { if let scaledImage = scaledImage {
...@@ -68,9 +70,9 @@ extension CNContactVCardSerialization { ...@@ -68,9 +70,9 @@ extension CNContactVCardSerialization {
if let compressionSize = compressedSize, image.count > compressionSize { if let compressionSize = compressedSize, image.count > compressionSize {
// compress image before sending vCard // compress image before sending vCard
guard let compressedImage = UIImage(data: image)?.convertToData(ofMaxSize: compressionSize)else { guard let compressedImage = UIImage(data: image)?
vcData = vcString.data(using: .utf8)! .convertToData(ofMaxSize: compressionSize)else {
return vcData return vCardString.data(using: .utf8)!
} }
image = compressedImage image = compressedImage
...@@ -79,10 +81,8 @@ extension CNContactVCardSerialization { ...@@ -79,10 +81,8 @@ extension CNContactVCardSerialization {
let base64Image = image.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0)) let base64Image = image.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
let vcardImageString = photofieldName.rawValue + base64Image + "\n" let vcardImageString = photofieldName.rawValue + base64Image + "\n"
vcString = vcString.replacingOccurrences(of: VCardFields.end.rawValue, with: (vcardImageString + VCardFields.end.rawValue)) vCardString = vCardString.replacingOccurrences(of: VCardFields.end.rawValue, with: (vcardImageString + VCardFields.end.rawValue))
vcData = vcString.data(using: .utf8)!
return vcData return vCardString.data(using: .utf8)!
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment