Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching CNcontact and Digits Find Friends Swift 3

I am trying to build iPhone App with digits Find a friend feature

I can get list of matching digitUserID from Digits.

Now I am struggling to match UserID and CNContacts.

Please point any examples to deal this.

As update:

do 
{
    try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey])) {
        (contact, cursor) -> Void in

        self.results.append(contact)
    }
}
catch{
    print("Handle the error please")
}

The above I have managed to get all contact but I don't know how to pass a phone number filter into this and get exact contact match with CNContact

like image 850
Kaviyarasu Arasu Avatar asked Dec 18 '22 14:12

Kaviyarasu Arasu


1 Answers

Ideally, one would have expected predicate of the CNContactFetchRequest to do the job, but that (still; argh) only accepts a narrow list of predicates defined with CNContact (e.g. CNContact predicateForContacts(matchingName:) or predicateForContacts(withIdentifiers:). It doesn't even accept the block-based NSPredicate.

So, you have to enumerate through, looking for matches yourself, e.g.

let request = CNContactFetchRequest(keysToFetch: [
    CNContactGivenNameKey as CNKeyDescriptor,
    CNContactFamilyNameKey as CNKeyDescriptor,
    CNContactMiddleNameKey as CNKeyDescriptor,
    CNContactEmailAddressesKey as CNKeyDescriptor,
    CNContactPhoneNumbersKey as CNKeyDescriptor
])

do {
    try contactStore.enumerateContacts(with: request) { contact, stop in
        for phone in contact.phoneNumbers {
            // look at `phone.value.stringValue`, e.g.

            let phoneNumberDigits = String(phone.value.stringValue.characters.filter { String($0).rangeOfCharacter(from: CharacterSet.decimalDigits) != nil })

            if phoneNumberDigits == "8885551212" {
                self.results.append(contact)
                return
            }
        }
    }
} catch let enumerateError {
    print(enumerateError.localizedDescription)
}

Regarding matching "digit UserID", I don't know what that identifier is (is it a Contacts framework identifier or Digits' own identifier?).

like image 144
Rob Avatar answered Jan 15 '23 12:01

Rob