3

I have a custom User class which stores the phone number of the user.

class User {
  let phoneNumber: String
}

How do I get the corresponding contact from the users contact book?

I tried the following but it seems like this works just for the contacts name because I'm always getting nil:

let predicate = CNContact.predicateForContactsMatchingName(userInstance.phoneNumber)
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactPhoneNumbersKey]

// Is already permitted
try! CNContactStore().unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch).first  // This returns nil

I've searched in the docs but I didn't find a proper solution.

Tobias
  • 4,523
  • 2
  • 20
  • 40

3 Answers3

5
let contactStroe = CNContactStore()
        let keysToFetch = [
            CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
            CNContactEmailAddressesKey,
            CNContactPhoneNumbersKey,
            CNContactImageDataAvailableKey,
            CNContactThumbnailImageDataKey]
contactStroe.requestAccessForEntityType(.Contacts, completionHandler: { (granted, error) -> Void in
if granted {
                let predicate = CNContact.predicateForContactsInContainerWithIdentifier(contactStroe.defaultContainerIdentifier())
                var contacts: [CNContact]! = []
                do {
                    contacts = try contactStroe.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch)// [CNContact]
                }catch {

                }
for contact in contacts {
                    var phoneStr = ""
                    var nameStr = ""
                    var number: CNPhoneNumber!
                    if contact.phoneNumbers.count > 0 {
                        number = contact.phoneNumbers[0].value as! CNPhoneNumber
                        phoneStr = number.stringValue.stringByReplacingOccurrencesOfString("-", withString: "")
                    }
                    nameStr = contact.familyName + contact.givenName
                    if !nameStr.isEmpty && !phoneStr.isEmpty {
                        let friend = YFriendsModel()
                        friend.name = nameStr
                        friend.phone = phoneStr
                        self.friendArr.append(friend)
                    }

                }

        })

this is my way, you can have a test

xiaoming
  • 130
  • 9
  • This looks like a good way but I wonder if there is a more efficient way than fetching all contacts and iterating over them. What if the user has 1000 or even more contacts? This will probably pretty slow. – Tobias Jan 17 '16 at 13:35
  • I really ignore its efficient just now,you can write these into other thread – xiaoming Jan 17 '16 at 13:56
  • Thanks, just what I needed – Oblivionkey3 Feb 19 '18 at 15:18
3

You can't.

This is a stupid solution as a huge workaround.

  1. Read each contact
  2. Normalize the phone number (not the easiest thing to do!)
  3. Cache contacts into a [String : Contact]

Then you can lookup contacts with contacts[phone_number]?

William Entriken
  • 37,208
  • 23
  • 149
  • 195
0

Swift 3 A nice solution, taking care also of efficiency:

    func getAllContacts() {
        let status = CNContactStore.authorizationStatus(for: CNEntityType.contacts) as CNAuthorizationStatus

        if status == CNAuthorizationStatus.denied {
            self.showAccessContactsDeniedAlert()
            return
        }
        let contactStore = CNContactStore()
        let keysToFetch = [
            CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
            CNContactEmailAddressesKey,
            CNContactPhoneNumbersKey,
            CNContactImageDataAvailableKey,
            CNContactThumbnailImageDataKey] as [Any]
        let request = CNContactFetchRequest(keysToFetch:keysToFetch as! [CNKeyDescriptor])
        do {
        try contactStore.enumerateContacts(with: request, usingBlock: { (contact:CNContact, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
            print(contact)
            for email in contact.emailAddresses {
                var dict = [String:String]()
                dict["name"] = contact.familyName + contact.givenName
                dict["email"] = email.value
                self.allContacts.add(dict)
            }
        })
        } catch {
            //catch
        }
    }

In this case I save name and email into a dict and I add it to a class variable called allContacts.

Note that a contact can have more than one email, so I create a dict for any email address in this case

Andrea.Ferrando
  • 987
  • 13
  • 23