使用swift 3中的CNContact按字母顺序加载联系人列表

时间:2017-04-26 04:23:13

标签: ios swift xcode cncontact

我有一个包含列表的联系人已被提取。 这是我的示例代码

func getData() -> [CNContact]  {
    let contactStore = CNContactStore()
    let keysToFetch = [
        CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
        CNContactEmailAddressesKey,
        CNContactDatesKey
        ] as [Any]
    var allContainers: [CNContainer] = []
    do {
        allContainers = try contactStore.containers(matching: nil)
    } catch {
        print("err")
    }

    var listContacts: [CNContact] = []
    for container in allContainers {
        let fetchPredecate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
        do {
            let containerResult = try contactStore.unifiedContacts(matching: fetchPredecate, keysToFetch: keysToFetch as! [CNKeyDescriptor] )
            listContacts.append(contentsOf: containerResult)
        } catch {

        }
    }

    // sort by name given
    let result = listContacts.sorted(by: {
        (firt: CNContact, second: CNContact) -> Bool in firt.givenName < second.givenName
    })

    return result
}

我需要加载到UITableView看起来像这样

所以,我该怎么做?谢谢 ! p / s:抱歉,我的英文不好

0 个答案:

没有答案