Swift Using Contacts Framework, search using phone number to get Name and User Image
Asked Answered
J

4

13

I have an API that returns phone numbers in format: +1415xxxxxxx (E164)

Right now these numbers are put into a Cell of a UITableView and are presented as expected, however I'd like to be able to search the users contacts on the phone to see if the there is a match - if so also pass back the Firstname, last name and known photo.

Looking at the Apple pages (https://developer.apple.com/library/watchos/documentation/Contacts/Reference/Contacts_Framework/index.html) I need to

 import ContactsUI

but then Im unsure, do I load the contactDB into a dictionary and then search it? I can find lots of things on searching via name and less on searching via number:

  let predicate = CNContact.predicateForContactsMatchingName("Sam") 

Im trying to get to a function that I can call, that searches using the PhoneNumber and gives me back the FirstName, FamilyName and Image.

  func searchForContactUsingNumber(PhoneNumber: String)
 {

 // Search Via phoneNumber
  let store = CNContactStore()
  let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingPhoneNumber(PhoneNumber), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey,CNContactImageData])

  return FirstName, GivenName,UIImage

 }

I get the feeling I'm going about this backwards but not sure which way is forwards.. Any ideas?

Jayme answered 12/4, 2016 at 19:43 Comment(6)
What happens when you try your code? What errors do you get and what is not working?Giliana
The code isn't right at all, I dont think there is a CNContact.predicateForContactsMatchingPhoneNumber, I just adapted Name to illustrate what Im trying to get too.Jayme
There does not appear to be a predicate that allows you to pass in a phone number and return a contact. Probably because this is a new API. I have written some code that returns a list of contact identifiers that match a phone number which you could then use to return a contact. Is this of interest?Giliana
Actually I have just realised that I can trivially return a list of contacts instead. You could then extract all the required information from that contact array. Is this of interest to you?Giliana
Yes Please :) That would be most helpfulJayme
OK - I have to go out but I will add an answer later (at least 4 hours but maybe more).Giliana
G
13

In order to get this example up-and-running quickly I used the following sources of info:

Filter non-digits from string

https://mcmap.net/q/906152/-swift-ios9-new-contacts-framework-how-to-retrieve-only-cncontact-that-has-a-valid-email-address

http://www.appcoda.com/ios-contacts-framework/

The code block below includes the authorisation check because I had to get it working in order to test in the simulator. The code is just the Single-View Apps view controller and you can connect up a UIButton in the Storyboard to the findContactInfoForPhoneNumber: method to get if to run. Output is to the console - you will need to replace these print statements with something else.

If you are not interested in the full view controller code then just look at the searchForContactUsingPhoneNumber(phoneNumber: String) method. I've followed Apple's advice in the docs to run the CNContact framework asynchronously.

The code strips all the +, - and ( symbols that could be in a phone number and just matches the digits so the phone number you pass in to match MUST be exactly the same.

//
//  ViewController.swift
//  ContactsTest
//
//  Created by Robotic Cat on 13/04/2016.
//

import UIKit
import Contacts

class ViewController: UIViewController {

    // MARK: - App Logic
    func showMessage(message: String) {
        // Create an Alert
        let alertController = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert)

        // Add an OK button to dismiss
        let dismissAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
        }
        alertController.addAction(dismissAction)

        // Show the Alert
        self.presentViewController(alertController, animated: true, completion: nil)
    }

    func requestForAccess(completionHandler: (accessGranted: Bool) -> Void) {
        // Get authorization
        let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)

        // Find out what access level we have currently
        switch authorizationStatus {
        case .Authorized:
            completionHandler(accessGranted: true)

        case .Denied, .NotDetermined:
            CNContactStore().requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
                if access {
                    completionHandler(accessGranted: access)
                }
                else {
                    if authorizationStatus == CNAuthorizationStatus.Denied {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
                            self.showMessage(message)
                        })
                    }
                }
            })

        default:
            completionHandler(accessGranted: false)
        }
    }

    @IBAction func findContactInfoForPhoneNumber(sender: UIButton) {

        self.searchForContactUsingPhoneNumber("(888)555-1212)")
    }

    func searchForContactUsingPhoneNumber(phoneNumber: String) {

        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), { () -> Void in
            self.requestForAccess { (accessGranted) -> Void in
                if accessGranted {
                    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey, CNContactPhoneNumbersKey]
                    var contacts = [CNContact]()
                    var message: String!

                    let contactsStore = CNContactStore()
                    do {
                        try contactsStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: keys)) {
                            (contact, cursor) -> Void in
                            if (!contact.phoneNumbers.isEmpty) {
                                let phoneNumberToCompareAgainst = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                                for phoneNumber in contact.phoneNumbers {
                                    if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                                        let phoneNumberString = phoneNumberStruct.stringValue
                                        let phoneNumberToCompare = phoneNumberString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                                        if phoneNumberToCompare == phoneNumberToCompareAgainst {
                                            contacts.append(contact)
                                        }
                                    }
                                }
                            }
                        }

                        if contacts.count == 0 {
                            message = "No contacts were found matching the given phone number."
                        }
                    }
                    catch {
                        message = "Unable to fetch contacts."
                    }

                    if message != nil {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.showMessage(message)
                        })
                    }
                    else {
                        // Success
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            // Do someting with the contacts in the main queue, for example
                            /*
                             self.delegate.didFetchContacts(contacts) <= which extracts the required info and puts it in a tableview
                             */
                            print(contacts) // Will print all contact info for each contact (multiple line is, for example, there are multiple phone numbers or email addresses)
                            let contact = contacts[0] // For just the first contact (if two contacts had the same phone number)
                            print(contact.givenName) // Print the "first" name
                            print(contact.familyName) // Print the "last" name
                            if contact.isKeyAvailable(CNContactImageDataKey) {
                                if let contactImageData = contact.imageData {
                                    print(UIImage(data: contactImageData)) // Print the image set on the contact
                                }
                            } else {
                                // No Image available

                            }
                        })
                    }
                }
            }
        })
    }

}
Giliana answered 14/4, 2016 at 20:12 Comment(4)
So Im very close to getting this. If I copy your code into my swift page, and run: self.searchForContactUsingPhoneNumber("+1987654321") // Where +1987654321 has an address entry it gives me that address entry's name and UIIamge details. As expected, Thank you! However if I put the same code into the func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { function, I immediately get error: [] fatal error: Index out of range Where its not loaded the contacts array. I was going to return the name and UI image using a tuple.Jayme
I can't tell what's going wrong from the comment but I can guess. The code clearly works but it sounds like you have a logic issue where you return the contact data. The way the code works is that you add the found contact into your data model and then update the table with reloadData or reloadRowsAtIndexPaths:withRowAnimation: rather than returning a value. This is because the code runs asynchronously outside the main queue. I recommend you use the debugger and find where the code crashes but it sounds like you are trying to access the contacts value before the code has returned.Giliana
That was my thoughts, because the job is running in a different thread and completes at a different time. I'll keep going. It clearly works, Thank you :)Jayme
If you can't solve it then ask a new question and post the relevant code.Giliana
C
2

ContactList with ContactUI Framework with Custom Tableview

import UIKit

class ContactCell: UITableViewCell {
    @IBOutlet weak var PersonNameLabel: UILabel!

    @IBOutlet weak var PersonMobileNOLabel: UILabel!

    @IBOutlet weak var PersonImage: UIImageView!

    @IBOutlet weak var PersonEmailLabel: UILabel!
}

ContactViewController

import ContactsUI

class ContactViewController: UIViewController,CNContactPickerDelegate,UITableViewDelegate,UITableViewDataSource{
var objects  = [CNContact]() 
@IBOutlet weak var tableView: UITableView! 
override func viewDidLoad() {
    super.viewDidLoad()
  self.getContacts()
}
func getContacts() {
    let store = CNContactStore()

    switch CNContactStore.authorizationStatus(for: .contacts){
    case .authorized:
        self.retrieveContactsWithStore(store: store)

    // This is the method we will create
    case .notDetermined:
        store.requestAccess(for: .contacts){succeeded, err in
            guard err == nil && succeeded else{
                return
            }
            self.retrieveContactsWithStore(store: store)

        }
    default:
        print("Not handled")
    }

}
func retrieveContactsWithStore(store: CNContactStore)
{


 let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey,CNContactImageDataKey, CNContactEmailAddressesKey] as [Any]
    let request = CNContactFetchRequest(keysToFetch: keysToFetch as! [CNKeyDescriptor])
    var cnContacts = [CNContact]()
    do {
        try store.enumerateContacts(with: request){
            (contact, cursor) -> Void in
            if (!contact.phoneNumbers.isEmpty) {
            }

            if contact.isKeyAvailable(CNContactImageDataKey) {
                if let contactImageData = contact.imageData {
                    print(UIImage(data: contactImageData)) // Print the image set on the contact
                }
            } else {
                // No Image available

            }
            if (!contact.emailAddresses.isEmpty) {
            }
            cnContacts.append(contact)
             self.objects = cnContacts
        }
    } catch let error {
        NSLog("Fetch contact error: \(error)")
    }

    NSLog(">>>> Contact list:")
    for contact in cnContacts {
        let fullName = CNContactFormatter.string(from: contact, style: .fullName) ?? "No Name"
        NSLog("\(fullName): \(contact.phoneNumbers.description)")
    }
   self.tableView.reloadData()   
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.objects.count
}
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! ContactCell  
    let contact = self.objects[indexPath.row]
print("theis my contact arrau \(self.objects.count)")
    let formatter = CNContactFormatter()  
    cell.PersonNameLabel.text = formatter.string(from: contact )
if let actualNumber = contact.phoneNumbers.first?.value as? CNPhoneNumber {
    //Get the label of the phone number

    //Strip out the stuff you don't need
    print(actualNumber.stringValue)  
cell.PersonMobileNOLabel.text = actualNumber.stringValue
}
else{
    cell.PersonMobileNOLabel.text = "N.A "
}
if let actualEmail = (contact as AnyObject).emailAddresses?.first?.value as String? {
    print(actualEmail)  
    cell.PersonEmailLabel.text = actualEmail
}
else{
    cell.PersonEmailLabel.text = "N.A "
}
if let imageData = contact.imageData {
    //If so create the image
    let userImage = UIImage(data: imageData)
    cell.PersonImage.image = userImage;
}

else{
    cell.PersonImage.image = UIImage (named: "N.A")
}
    return cell
}
}
Confiscatory answered 12/7, 2017 at 9:39 Comment(0)
K
2

import UIKit import Contacts

class PhonebookVC: UIViewController ,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var ContactTblView: UITableView!
@IBOutlet weak var SearchTxtField: SkyFloatingLabelTextField!



var contacts = [CNContact]()
var NameArray = [String]()
var NumberArray = [String]()

var filteredName = [String]()
var filteredNumber = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
    getContacts()
    SearchTxtField.delegate = self
    // Do any additional setup after loading the view.
}

@IBAction func SearchFunc(_ sender: UIButton) {

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.filteredName.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = ContactTblView.dequeueReusableCell(withIdentifier: "cell") as! PhonebookCell
    cell.ContactName.text = self.filteredName[indexPath.row]
    cell.ContactNumber.text = self.filteredNumber[indexPath.row]
    return cell
}




/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

extension PhonebookVC {

func getContacts()
{
    let status = CNContactStore.authorizationStatus(for: .contacts)
    if status == .denied || status == .restricted {
        presentSettingsActionSheet()
        return
    }

    // open it

    let contactStore = CNContactStore()
    contactStore.requestAccess(for: .contacts) { granted, error in
        guard granted else {
            DispatchQueue.main.async {
                self.presentSettingsActionSheet()
            }
            return
        }

        // get the contacts
        let keys = [
            CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
            CNContactPhoneNumbersKey as CNKeyDescriptor] as [Any]
        let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
        do {
            try contactStore.enumerateContacts(with: request){
                (contact, stop) in
                // Array containing all unified contacts from everywhere
                self.contacts.append(contact)
                var i = 0
                for phoneNumber in contact.phoneNumbers {

                        print("\(contact.givenName) \(contact.familyName)\n \(phoneNumber.value.stringValue)")

                        self.NameArray.append("\(contact.givenName) \(contact.familyName)")
                        self.NumberArray.append(phoneNumber.value.stringValue)
                        i = i+1

                }
                i = 0

                self.filteredName   = self.NameArray
                self.filteredNumber = self.NumberArray

                self.ContactTblView.reloadData()
            }
        } catch {
            print("unable to fetch contacts")
        }
    }
}

func presentSettingsActionSheet() {
    let alert = UIAlertController(title: "Permission to Contacts", message: "This app needs access to contacts in order to Send top-up.", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { _ in
        let url = URL(string: UIApplication.openSettingsURLString)!
        UIApplication.shared.open(url)
    })
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    present(alert, animated: true)
}

}

extension PhonebookVC : UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { self.filteredName.removeAll() self.filteredNumber.removeAll()

    if(self.NameArray.count != 0){


        var mystring = "\(textField.text ?? "")\(string)"
        if(textField.text?.count == 1 && string == ""){

            mystring = ""
        }
        var i = 0
        for ContactName in self.NameArray {
            let name = ContactName
            let range = name.lowercased().range(of: mystring, options: .caseInsensitive, range: nil,   locale: nil)

            if range != nil {
                if(filteredName.count == 0){

                    filteredName = [ContactName]
                    filteredNumber = [NumberArray[i]]

                }else{

                    filteredName.append(ContactName)
                    filteredNumber.append(NumberArray[i])
                }

            }
            i = i+1
        }

        if(string == "" && (textField.text?.count == 1)){

            self.filteredName   = self.NameArray
            self.filteredNumber = self.NumberArray

        }

        self.ContactTblView.reloadData()
    }
    return true
}

}

Karyolysis answered 21/6, 2019 at 7:45 Comment(0)
S
0

The correct way is to index phone numbers in your own database so you can lookup for the contact identifier.

Spiritualize answered 19/10, 2017 at 18:52 Comment(2)
Can someone please confirm this?Lolly
I don't think soDunnite

© 2022 - 2024 — McMap. All rights reserved.