在ios Swift中获取所有联系人? [英] Fetching all contacts in ios Swift?

查看:275
本文介绍了在ios Swift中获取所有联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ios swift具有 Contacts Framework ,我可以在其中获取联系人,但是找不到任何方法可以一起获取所有联系人,因此我可以从该数组访问每个联系人.所有用于获取联系人的方法似乎都需要某种条件.有什么方法可以将所有联系人聚集在一起?

I am aware of the ios swift has a Contacts Framework where I can fetch contacts, but I cannot find any method to fetch all the contacts together where I can access each of the contacts from that array. All methods for fetching contacts seems to require some sort of conditions. Is there any method where I can get all the contacts together?

谢谢

推荐答案

对Contact Framework问题的许多答案都建议对各种容器(帐户)进行迭代.但是,Apple的文档将统一联系人"描述为

Many answers to Contact Framework questions suggest iterating over various containers (accounts). However, Apple's documentation describes a "Unified Contact" as

不同帐户中代表同一个人的联系人可能会自动链接在一起.链接的联系人在OS X和iOS应用中显示为统一联系人.统一联系人是合并到一个联系人中的一组链接联系人的内存临时视图.

Contacts in different accounts that represent the same person may be automatically linked together. Linked contacts are displayed in OS X and iOS apps as unified contacts. A unified contact is an in-memory, temporary view of the set of linked contacts that are merged into one contact.

默认情况下,Contacts框架返回统一的联系人.每个提取的统一联系人(CNContact)对象都有其自己的唯一标识符,该标识符不同于链接的联系人集中的任何单个联系人的标识符.应使用其标识符对统一联系人进行重新提取. 来源

By default the Contacts framework returns unified contacts. Each fetched unified contact (CNContact) object has its own unique identifier that is different from any individual contact’s identifier in the set of linked contacts. A refetch of a unified contact should be done with its identifier. Source

最简单的获取单个数组中的(部分基于键的)联系人列表的方法如下:

So simplest way to fetch a list of (partial, based on keys) contacts in a single array, would be the following:

    var contacts = [CNContact]()
    let keys = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)]
    let request = CNContactFetchRequest(keysToFetch: keys)

    do {
        try self.contactStore.enumerateContactsWithFetchRequest(request) {             
            (contact, stop) in
            // Array containing all unified contacts from everywhere
            contacts.append(contact)
        }
    } 
    catch {
        print("unable to fetch contacts")
    }

这篇关于在ios Swift中获取所有联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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