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 {
case photoJPEG = "PHOTO;TYPE=JPEG;ENCODING=BASE64:"
case photoPNG = "PHOTO;TYPE=PNG;ENCODING=BASE64:"
case end = "END:VCARD"
case name = "N:"
case fullName = "FN:"
}
extension CNContactVCardSerialization {
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 {
return vcData
}
let entryUID = VCardFields.uid.rawValue + contact.identifier
vcString = vcString.replacingOccurrences(of: VCardFields.begin.rawValue,
with: (VCardFields.begin.rawValue +
"\n" + entryUID))
var vCardString = beginString + entryUIDString + firstnameString + fullNameString + endString
// if contact have an image add it to vCard data
guard var image = contact.imageData else {
vcData = vcString.data(using: .utf8)!
return vcData
return vCardString.data(using: .utf8)!
}
var photofieldName = VCardFields.photoPNG
......@@ -57,7 +58,8 @@ extension CNContactVCardSerialization {
// if we need smallest image first scale it and than compress
var scaledImage: UIImage?
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 {
......@@ -68,9 +70,9 @@ extension CNContactVCardSerialization {
if let compressionSize = compressedSize, image.count > compressionSize {
// compress image before sending vCard
guard let compressedImage = UIImage(data: image)?.convertToData(ofMaxSize: compressionSize)else {
vcData = vcString.data(using: .utf8)!
return vcData
guard let compressedImage = UIImage(data: image)?
.convertToData(ofMaxSize: compressionSize)else {
return vCardString.data(using: .utf8)!
}
image = compressedImage
......@@ -79,10 +81,8 @@ extension CNContactVCardSerialization {
let base64Image = image.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
let vcardImageString = photofieldName.rawValue + base64Image + "\n"
vcString = vcString.replacingOccurrences(of: VCardFields.end.rawValue, with: (vcardImageString + VCardFields.end.rawValue))
vcData = vcString.data(using: .utf8)!
vCardString = vCardString.replacingOccurrences(of: VCardFields.end.rawValue, with: (vcardImageString + VCardFields.end.rawValue))
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