-
Notifications
You must be signed in to change notification settings - Fork 40
Quick Start (Objective C)
In this walkthrough, we will set up a data source that provides access to all contacts from the user's address book that have a phone number. This walkthrough uses Objective-C, but there is also a Swift version available.
We will be using the Apple-provided Contacts framework as the source of our data. Ohana comes with a data provider that pulls from the address book, the OHCNContactsDataProvider
.
OHCNContactsDataProvider *dataProvider = [[OHCNContactsDataProvider alloc] initWithDelegate:self];
The data provider requires that we pass in a delegate conforming to the OHCNContactsDataProviderDelegate
protocol. This delegate handles requesting authentication. We will request system authentication using one of the functions in the Contacts framework.
- (void)dataProviderDidHitAuthenticationChallenge:(OHCNContactsDataProvider *)dataProvider
{
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (granted) {
[dataProvider loadContacts];
}
}];
}
First, we will create an alphabetical sort post processor to sort our contacts by full name.
OHAlphabeticalSortPostProcessor *alphabeticalSortProcessor = [[OHAlphabeticalSortPostProcessor alloc] initWithSortMode:OHAlphabeticalSortPostProcessorSortModeFullName];
Next, we will filter out any contacts that don't have a phone number.
OHRequiredFieldPostProcessor *phoneNumberProcessor = [[OHRequiredFieldPostProcessor alloc] initWithFieldType:OHContactFieldTypePhoneNumber];
We are now ready to set up our data source using the data provider and post processors that we have created.
OHContactsDataSource *dataSource = [[OHContactsDataSource alloc] initWithDataProviders:[NSOrderedSet orderedSetWithObjects:dataProvider, nil]
postProcessors:[NSOrderedSet orderedSetWithObjects:alphabeticalSortProcessor, phoneNumberProcessor, nil]];
Now we'll add ourselves as an observer to the data source's ready signal so we can do something with the contacts when they're loaded.
[dataSource.onContactsDataSourceReadySignal addObserver:self callback:^(typeof(self) self) {
// do something with dataSource.contacts
}];
Now you're ready to start the loading process and get some contacts.
[dataSource loadContacts];