如何在CNContactVCardSerialization中使用方法dataWithContacts? [英] How to use method dataWithContacts in CNContactVCardSerialization?

查看:262
本文介绍了如何在CNContactVCardSerialization中使用方法dataWithContacts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用联系人的vCard表示形式获取NSData对象。我的代码:

I am trying get NSData object with the vCard representation of the contact. My code:

let contactStore = CNContactStore()
let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey])

var contacts = [CNContact]()
var vcard = NSData()

    do{
            try contactStore.enumerateContactsWithFetchRequest(fetchRequest) { (contact, status) -> Void in
            self.fetchRequest.unifyResults = true
            self.contacts.append(contact)}

    } catch {
            print("Error \(error)")
      }


    do {
           try vcard = CNContactVCardSerialization.dataWithContacts(contacts)
    } catch {
          print("Error \(error)")
      } 

但是,我得到了错误:


将联系人写入vCard(数据)的异常:获取联系人时未请求属性。错误NilError。

Exception writing contacts to vCard (data): A property was not requested when contact was fetched. Error NilError.

我了解访问联系人的错误,但如何解决?

I understand that the error in the access to contacts, but how to fix it?

推荐答案

我找到了解决方案并且有效:

I found the solution and it work:

let contactStore = CNContactStore()
var contacts = [CNContact]()
var vcardFromContacts = NSData()

let fetchRequest = CNContactFetchRequest(keysToFetch:[CNContactVCardSerialization.descriptorForRequiredKeys()])

do{
    try contactStore.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: {
    contact, cursor in
    self.contacts.append(contact)})
} catch {
    print("Get contacts \(error)")
}

// Returns the vCard representation of the specified contacts

do {
    try vcardFromContacts = CNContactVCardSerialization.dataWithContacts(contacts)
} catch {
    print("vcardFromContacts \(error)")
}

但是,我返回了电子名片数据中的联系人:

But, whet i returns the contacts from the vCard data:

do {
    try contactsFromVcard = CNContactVCardSerialization.contactsWithData(vcardFromContacts)
} catch {
    print("contactsFromVcard \(error)")
}

字段联系人imageData为零。虽然不是零。

field contact imageData has nil. Though it not nil.

这篇关于如何在CNContactVCardSerialization中使用方法dataWithContacts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆