ios - CNContact 属性编码

标签 ios swift cncontact

我有一个名为 André 的 v-card 字符串,我用 v-card 初始化了一个 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 中获取联系人框架的正确字符串?

最佳答案

您需要为 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 中的这种特定类型的错误,那么您可以手动将字符集注入(inject)其中:

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

关于ios - CNContact 属性编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46287505/

相关文章:

ios - 服务器 IOS 上 MOV 的缩略图

ios - 从 for 循环中调用时看不到 CABasicAnimation

Swift 3 UITableView 部分显示在列表后面

iOS - Xcode 错误 : cannot attach to process due to System Integrity Protection

ios - CNContact 添加新的联系人问题

ios - 如何从 CNContactStore ios 获取 RecordID

iphone - 限制共享联系人时打开的内容

IOS:在触摸 UITableView 时隐藏键盘

ios - 尝试使用闭包在 ViewController 之间传递数据时我犯了什么错误?

c# - 如何使用 Alamofire POST 请求将图像作为 Base64String 发送以及如何在 Asp.net MVC4 web Api Controller 中处理请求?