CNUI ERROR Selection predicates are set but the de

2019-02-19 16:56发布

问题:

I try to use the new iOS 9.0 CNContactPickerViewController to select a contact in objective-C. I set the delegate and implement the CNContactPickerDelegate methods.

@import ContactsUI;
@import Contacts;
//-----------------------------------------------------------------------
- (void) presentContacts
{
    CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
    contactPicker.delegate = self;
    contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:@"familyName LIKE[cd] 'smith'"];
    contactPicker.predicateForSelectionOfContact = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
    [_myViewController presentViewController:contactPicker animated:YES completion:nil];
}
//-----------------------------------------------------------------------
- (void) contactPickerDidCancel: (CNContactPickerViewController *) picker
{
    NSLog(@"didCancel");
}
//-----------------------------------------------------------------------
- (void) contactPicker: (CNContactPickerViewController *) picker
      didSelectContact: (CNContact *)                     contact
{
    NSLog(@"didSelectContact"):
}
//-----------------------------------------------------------------------
- (void)      contactPicker: (CNContactPickerViewController *) picker
   didSelectContactProperty: (CNContactProperty *)             contactProperty
{
    NSLog(@"didSelectProperty");
}
//-----------------------------------------------------------------------

The contacts picker is presented with 'smith' selectable but I get the following message:

[CNUI ERROR] Selection predicates are set but the delegate does not implement contactPicker:didSelectContact: and contactPicker:didSelectContactProperty:. Those predicates will be ignored.

And I never get any log from the delegate methods. It behaves exactly as the line

    contactPicker.delegate = self;

is ignored. Even I click on the "cancel" button in the picker, I don't get my "didCancel" message but I get another message:

plugin com.apple.MobileAddressBook.ContactsViewService invalidated

I found in https://forums.developer.apple.com/thread/12275 somebody with the similar problem in swift and he solved it telling us: "So I found that the ContactsPicker I was calling was in the wrong module" but I do not understand how we can get the wrong module and how to call the "right" module.

I have the same problem on the simulator and on a real device (iPad).

回答1:

Thanks to Joel in my related question With CNContactPickerViewController in iOS 9.0, how to enable/disable single or multiple selection?, I found that I just forgot to store the CNContactPickerViewController in a property that exists the time the user make the selection.

So my code becomes:

- (void) presentContacts
{
    _contactPicker = [[CNContactPickerViewController alloc] init];
    contactPicker.delegate = self;
    ...
}