问题
I use below code to show contact list in my application:
self.contactPicker = [[CNContactPickerViewController alloc] init];
self.contactPicker.delegate = self;
self.contactPicker.predicateForSelectionOfContact =
[NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
[viewController presentViewController:self.contactPicker
animated:YES
completion:nil
];
The page shows all contacts including emails . I want to only show numbers (and no emails) is there any options on the NSPredicate or CNContactPickerViewController itself to show only numbers?
I also used:
self.contactPicker.displayedPropertyKeys = @[@"phoneNumber"];
But it is not working too.
回答1:
displayedPropertyKeys use this property to control what all fields should be visible.
回答2:
If you only want to show phone numbers for the contact when you select the contact, use the displayedPropertyKeys property:
self.contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
But if you don't want to even be able to select those contacts who don't have phone numbers, you'd want to set predicateForEnablingContact:
self.contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:@"%K.@count > 0", CNContactPhoneNumbersKey];
I notice that you're using self.contactPicker.predicateForSelectionOfContact. But what if the person has multiple phone numbers? Which one are you going to use? Personally, I'd be inclined to not set that property at all, or if you wanted to, perhaps allow the user to drill in and choose the desired phone number if there were multiple phone numbers:
self.contactPicker.predicateForSelectionOfContact = [NSPredicate predicateWithFormat:@"%K.@count == 1", CNContactPhoneNumbersKey];
来源:https://stackoverflow.com/questions/39700966/avoid-cncontactpickerviewcontroller-to-show-emails