我有一个名字为André的V卡字符串,并且我用V卡初始化了一个CNContact。
BEGIN:VCARD
VERSION:2.1
N:Foo;André;;;
FN:André Foo
TEL;CELL:00023 4474848
END:VCARD我使用原始字符串初始化联系人,如下所示:
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")
}
}但是当我打印出contact.givenName的原始字符串时,我得到:
André
如何在iOS中获取Contacts框架的正确字符串?
发布于 2017-09-19 04:35:03
您需要在vcard字段中添加一个字符集,默认为ASCII。
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8:Foo;André;;;
FN;CHARSET=UTF-8:André Foo
TEL;CELL:00023 4474848
END:VCARD如果您想在vcard中破解这种特定类型的错误,那么您可以手动将字符集注入其中:
let fixed = string.replacingOccurrences(of: "\nN:", with: "\nN;CHARSET=UTF-8:").replacingOccurrences(of: "\nFN:", with: "\nFN;CHARSET=UTF-8:")https://stackoverflow.com/questions/46287505
复制相似问题