【问题标题】:Swift 2.1 Searching Contacts by E-mail in iOS 9Swift 2.1 在 iOS 9 中通过电子邮件搜索联系人
【发布时间】:2015-11-20 21:55:34
【问题描述】:

在新的联系人框架中似乎有一种按名称搜索的方法:

let predicate = CNContact.predicateForContactsMatchingName("john")

let toFetch = [CNContactGivenNameKey, CNContactFamilyNameKey]

do {
     let contacts = try store.unifiedContactsMatchingPredicate(
          predicate, keysToFetch: toFetch)

      for contact in contacts{
          print(contact.givenName)
          print(contact.familyName)
          print(contact.identifier)
      }

} 
catch let err {
        print(err)
}

但没有明显的方法可以通过电子邮件进行搜索,我可以在文档或搜索中找到。

如何通过电子邮件地址搜索联系人?

这些文章herehere 对学习新框架很有帮助,但都没有揭示如何通过电子邮件进行搜索。

【问题讨论】:

  • 您的问题得到解答了吗?

标签: swift2 ios9


【解决方案1】:

遇到同样的问题,我设法找到了解决方案。我已经通过获取所有联系人然后遍历它们以找到匹配的联系人来解决这个问题。

代码肯定可以重构的。

import UIKit
import Contacts

public class ContactFinder: NSObject {

    private lazy var store = CNContactStore()
    private var allContacts: [CNContact] = []
    private let keysToFetch: [CNKeyDescriptor] = [CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactImageDataAvailableKey]

    public func requestAccess(completion:((success: Bool, error: NSError?)->())) {

        let status = CNContactStore.authorizationStatusForEntityType(.Contacts)

        if status == .NotDetermined {
            store.requestAccessForEntityType(.Contacts, completionHandler: { [weak self](success, error) -> Void in
                if success {
                    self?.getAllContacts(completion)
                } else {
                    completion(success: false, error: error)
                }
            })
        } else if status == .Authorized {
            getAllContacts(completion)
        } else {
            completion(success: false, error: nil)
        }
    }

    public func searchForContactUsingEmail(email: String, completion:((contacts: [CNContact])->())) {
        var contacts: [CNContact] = []
        for contact in allContacts {
            let em = contact.emailAddresses

            if em.count > 0 {
                let results = em.filter({ val in
                    let ce = (val.value as! String).trimmedString.lowercaseString

                    return ce == email.trimmedString.lowercaseString
                })

                if results.count > 0 {
                    contacts.append(contact)
                }
            }
        }

        completion(contacts: contacts)
    }

    private func getAllContacts(completion:((success: Bool, error: NSError?)->())) {
        let request = CNContactFetchRequest(keysToFetch: keysToFetch)
        request.predicate = nil
        request.unifyResults = true

        do {
            try store.enumerateContactsWithFetchRequest(request, usingBlock: { [weak self](contact, _) -> Void in
                self?.allContacts.append(contact)
                })
        } catch let e as NSError {
            print(e.localizedDescription)
            completion(success: false, error: e)
            return
        }

        completion(success: true, error: nil)
    }

}

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多