1

Is it possible to disable message, call, mail etc options from contact List using Address Book Api

I just want user can select only address from contact list.

For reference please check below marked option on image.

image

Prema Janoti
  • 836
  • 5
  • 18

1 Answers1

1

There is CNContact property keys to display in the contact detail card. You can do this by setting displayedPropertyKeys property of CNContactPickerViewController when you showing the contact list:

    let contactPicker = CNContactPickerViewController()
    contactPicker.displayedPropertyKeys = [CNContactPostalAddressesKey]

If you are showing the contact using CNContactViewController then can disable action using allowsActions property:

    let controller = CNContactViewController(for: SELECTED_CONTACT)
    controller.contactStore = CNContactStore()
    controller.allowsActions = false
    controller.delegate = self

Complete implemention:

Function for showing CNContactPickerViewController:

@IBAction func showContactList( sender: Any) ->Void{
    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self
    self.present(contactPicker, animated: true, completion: nil)
}

Implement delegate for CNContactPickerViewController:

public func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact){
    print(#function)
    picker.dismiss(animated: true, completion:{
        self.showDetailFor(contact: contact)
    })
}

to display details of contact open CNContactViewController:

func showDetailFor(contact: CNContact) -> Void{
    let contactViewController = CNContactViewController(forUnknownContact: contact)
    contactViewController.contactStore = CNContactStore()
    contactViewController.delegate = self
    contactViewController.allowsActions = false
    self.navigationController?.pushViewController(contactViewController, animated: true)
}

implement CNContactViewControllerDelegate to get selected property or handle action for any property:

func contactViewController(_ viewController: CNContactViewController,
                           shouldPerformDefaultActionFor property: CNContactProperty) -> Bool{
// return true if want to perform any action on selected property otherwise return false
    return false
}
Rocky
  • 2,903
  • 1
  • 22
  • 26
  • That is I am already using. I have to disable message, call, mail options which are marked on image. – Prema Janoti Jan 04 '18 at 06:23
  • Those buttons are still visible and enabled when you do this. – Rob Jan 04 '18 at 06:24
  • @Rob - You are Right, Is there any way to disable those buttons? – Prema Janoti Jan 04 '18 at 06:29
  • @PremaJanoti - did you check with edited answer for selected contact? – Rocky Jan 04 '18 at 07:33
  • @Rocky - Yes I checked your edited answer, But I am using CNContactPickerViewController not CNContactViewController. – Prema Janoti Jan 04 '18 at 07:35
  • @PremaJanoti - You could implement `didSelect` of the contact (preventing it from showing the `CNContactViewController` itself) and then do the above inside that `didSelect` method, thereby disabling the actions. – Rob Jan 04 '18 at 08:00
  • @Rocky - I think you're on the right track here. I wonder about `didSelect`, though, because in my tests, with `didSelect`, it's automatically dismissed (in fact, I really dislike that behavior), so I'm not sure you want to dismiss it twice. I just `DispatchQueue.main.async { showDetail(for: ...) }`. – Rob Jan 05 '18 at 00:04
  • @Rocky - Thanks for your efforts I really appreciate it, But it is not as per my requirement. I think it is not possible to disable those buttons (message, call, video and mail) in contract detail. – Prema Janoti Jan 05 '18 at 04:46