Frederic Adda
Frederic Adda

Reputation: 6092

iOS9 - CNContactPickerViewController : pre-select contacts

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: enter image description here

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

Upvotes: 6

Views: 2965

Answers (2)

Andrew Bennett
Andrew Bennett

Reputation: 940

Use a Predicate. Keep an array of the CNContact Identifiers that were previously selected and then send the predicate like this:

contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:@"!(identifier IN %@)", arrayOfPreviouslySelected];

and an example to only show contacts with email addresses:

contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:@"!(identifier IN %@) && emailAddresses.@count > 0", arrayOfPreviouslySelected];

Upvotes: 1

Aaron
Aaron

Reputation: 7145

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 CNKeyDescriptors 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

Upvotes: 1

Related Questions