I have a v-card string with the first name André
and I initialise a CNContact
with the v-card.
BEGIN:VCARD
VERSION:2.1
N:Foo;André;;;
FN:André Foo
TEL;CELL:00023 4474848
END:VCARD
I initialise the contact with the raw string like this:
if let data = string.data(using: .utf8) {
do {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
return contact
} catch {
print("Data is not a VCard")
}
}
But when I print out the raw string of contact.givenName
I get:
André
How can I get the proper string of the Contacts framework in iOS?
You need to add a charset to the vcard fields, it defaults to ASCII.
If you want to hack around this specific type of error in the vcard then you can inject the charset manually into it: