CNContact encoding of properties

倾然丶 夕夏残阳落幕 提交于 2019-12-02 19:47:40

问题


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?


回答1:


You need to add a charset to the vcard fields, it defaults to ASCII.

BEGIN:VCARD

VERSION:2.1

N;CHARSET=UTF-8:Foo;André;;;

FN;CHARSET=UTF-8:André Foo

TEL;CELL:00023 4474848

END:VCARD

If you want to hack around this specific type of error in the vcard then you can inject the charset manually into it:

let fixed = string.replacingOccurrences(of: "\nN:", with: "\nN;CHARSET=UTF-8:").replacingOccurrences(of: "\nFN:", with: "\nFN;CHARSET=UTF-8:")


来源:https://stackoverflow.com/questions/46287505/cncontact-encoding-of-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!