Is it possible to customize CNContactPickerViewController?

I open the CNContactPickerViewController for use in the application, but I want to change the way it is presented in accordance with the needs of my applications, preferably without rolling back on my own and without having to re-invent the wheel a lot. This is how I open it with Objective-C ....

self.contactPicker = [[CNContactPickerViewController alloc] init]; self.contactPicker.delegate = self; //Only enable contacts to be selected that have atleast one email address NSArray *propertyKeys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey]; NSPredicate *enablePredicate = [NSPredicate predicateWithFormat:@" emailAddresses.@count != 0"]; self.contactPicker.displayedPropertyKeys = propertyKeys; self.contactPicker.predicateForEnablingContact = enablePredicate; [self presentViewController:self.contactPicker animated:YES completion:nil]; 

When it opens, it currently looks like this:

Before setting up

However, due to an error in the SDK, searching for people in this form does not work, because you cannot select from the search results. I am going to make a mistake for this, but at the same time I want to hide the search bar first. I found some old questions about removing SearchBar, but they are related to ABPeoplePickerNavigationController and are not related to CNContacts. I also do not want to use groups, and if I could remove this button and move the Cancel button to the left, that would be great and would make the selection interface in my application cleaner. Here's what I would like to look like:

After setting

Can someone tell me if this is possible and maybe point me in the right direction? I have a delegate method for getting an array of contacts after selection, my problem is how it looks in the application.

Thanks in advance!

Plasma

+7
objective-c xcode uiviewcontroller
source share
2 answers

The real answer is the one you don't want to hear:

No, you cannot change the user interface that Apple presents (at least in a meaningful way). You may be able to change the color of the hue and other small details, but you cannot change what you like. If there is an error in the selector, you certainly cannot fix this, unfortunately. I would suggest that simply hiding the search bar is not a great option, since for large contact lists, searching is usually the main way users switch to contacts.

Lateral note - implemented a lot of controllers representing the Apple framework as a special kind of "remote control controller." The view controller does not actually start as part of your application, but in the sandbox of the parent application. This prevents any tricks, such as bypassing and changing the user interface hierarchy of these presented controllers. I wonโ€™t be surprised if this is so.

I would suggest that re-creating the contact selection view is not too complicated and gives you complete flexibility in terms of customization. I did it myself, and there were no serious obstacles to crossing. Even if you are new to iOS, this will be a great training exercise. For a good solution, you probably want to get all the contacts in the background thread and display the bootloader, as large contact databases may take a little time to retrieve. (Better yet, pre-select the contacts in the previous view and show only the download scan, if this selection is not yet complete).

If you donโ€™t like doing this, Iโ€™ve seen several contact selection schemes around a place on GitHub. I'm not sure what the quality is, but at least it could be a great starting point.

+1
source share

You can get an array of all contacts, and then display and work with it as you like:

 - (NSMutableArray<CNContact *> *)allContacts { NSMutableArray <CNContact *> *result = [NSMutableArray array]; NSError *error = nil; NSArray *keysToFetch = @[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]; CNContactStore *contactStore = [[CNContactStore alloc] init]; NSArray <CNContainer *> *allContainers = [contactStore containersMatchingPredicate:nil error:&error]; for (CNContainer *container in allContainers) { NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier]; NSArray *fetchedContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:&error]; [result addObjectsFromArray:fetchedContacts]; } return result;} 
0
source share

All Articles