I use the new CNContactPickerViewController to select contacts from the address book and use them in my app.
When I press a button, I call this code:
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
presentViewController(contactPicker, animated: true, completion: nil)
And I used the following CNContactPickerDelegate method to select multiple contacts:
func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]) {
selectedRecipients = contacts.map { $0.emailAddresses.first!.value as! String }
tableView.reloadData()
}
This helps me fill an array called "selectedRecipients" (basically, an arrray of emailAddresses Strings)
Here is the result:
That's working fine. However, if I had previously selected contacts, I'd like the contactPicker to pre-select contacts when I initialize it, that is, show contacts as already selected when it appears. Is there a way to do that?
Thanks
I think you're asking the Contacts Framework to manage some extra state that it isn't designed to handle. Your "selected" state flag isn't available on the CNContact
object (see list of CNKeyDescriptor
list below) and thus isn't available for display in the picker view controller.
It seems to me that if you want to add this extra state into your app you're going to need to roll your own solution.
List of CNKeyDescriptor
s as far as I can tell:
// Properties that are always fetched. Can be used with key value coding and observing.
@available(iOS 9.0, *)
public let CNContactIdentifierKey: String
// Optional properties that can be fetched. Can be used with key value coding and observing.
@available(iOS 9.0, *)
public let CNContactNamePrefixKey: String
@available(iOS 9.0, *)
public let CNContactGivenNameKey: String
@available(iOS 9.0, *)
public let CNContactMiddleNameKey: String
@available(iOS 9.0, *)
public let CNContactFamilyNameKey: String
@available(iOS 9.0, *)
public let CNContactPreviousFamilyNameKey: String
@available(iOS 9.0, *)
public let CNContactNameSuffixKey: String
@available(iOS 9.0, *)
public let CNContactNicknameKey: String
@available(iOS 9.0, *)
public let CNContactPhoneticGivenNameKey: String
@available(iOS 9.0, *)
public let CNContactPhoneticMiddleNameKey: String
@available(iOS 9.0, *)
public let CNContactPhoneticFamilyNameKey: String
@available(iOS 9.0, *)
public let CNContactOrganizationNameKey: String
@available(iOS 9.0, *)
public let CNContactDepartmentNameKey: String
@available(iOS 9.0, *)
public let CNContactJobTitleKey: String
@available(iOS 9.0, *)
public let CNContactBirthdayKey: String
@available(iOS 9.0, *)
public let CNContactNonGregorianBirthdayKey: String
@available(iOS 9.0, *)
public let CNContactNoteKey: String
@available(iOS 9.0, *)
public let CNContactImageDataKey: String
@available(iOS 9.0, *)
public let CNContactThumbnailImageDataKey: String
@available(iOS 9.0, *)
public let CNContactImageDataAvailableKey: String
@available(iOS 9.0, *)
public let CNContactTypeKey: String
@available(iOS 9.0, *)
public let CNContactPhoneNumbersKey: String
@available(iOS 9.0, *)
public let CNContactEmailAddressesKey: String
@available(iOS 9.0, *)
public let CNContactPostalAddressesKey: String
@available(iOS 9.0, *)
public let CNContactDatesKey: String
@available(iOS 9.0, *)
public let CNContactUrlAddressesKey: String
@available(iOS 9.0, *)
public let CNContactRelationsKey: String
@available(iOS 9.0, *)
public let CNContactSocialProfilesKey: String
@available(iOS 9.0, *)
public let CNContactInstantMessageAddressesKey: String
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With