2017-09-18 73 views
-1

我有一個名片字符串,名字爲André,我用v-card初始化一個CNContactCNContact屬性編碼

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中獲取適當的聯繫人框架字符串?

回答

-1

你需要添加一個字符集到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 

如果你想破解周圍的虛擬卡這一特定類型的錯誤,那麼你可以手動注入的字符集到它:

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

但如果我無法控制的字符集?假設我從第三方獲得vcard ... –

+0

然後,第三方需要修復他們的電子名片。 – dan

+0

這根本沒用,對不起。如果網絡就是這樣,我們現在都應該放棄... –