“Contacts Framework” came up with iOS 9.0 replacing the older Framework “AddressBook”. Those who have used the previous framework must have faced the difficulties to understand and manage the framework.
As new Contacts framework is brought up by Apple, we found it more straightforward and easier to manage too. It is much easier to understand even for naive developers. We can create, fetch and update a contact in no time. Also, the development time will get reduced while using this new Contacts framework and the code will be easier to manage and modify. So, we iOS developers, can breathe some air and play with the phone contacts.
Through this blog you will get to understand the difference between the new framework and the older AddressBook. Let us first quickly see what all we will cover in this blog.
What we will cover :
- How to ask for privacy permissions from user for getting contacts from the phone?
- How to fetch contacts from the device?
- How to save contacts to the device?
If your project is supporting iOS 8.0 then you will be using Addressbook framework and Contacts framework both. So, we will cover all this by using both Contacts framework and AddressBook framework to compare the differences between them.
So folks, let’s get started.
First, we will look at how to ask for permissions from the user for getting contact access.
There are four types of contact authorization states of the app represented by the CNAuthorizationStatus, which is an enum. The four values it can take are:
- Not determined: This is the state when the user installs the app and opens app for the first time. At this time the user has not allowed or denied the access to the device contacts.
- Denied: This is the state when the user has denied the access to the contacts before. Only user can change this from the device settings . So, in this case we can navigate user to the device settings to allow contact access.
- Restricted: In this case application cannot access the contacts and also user cannot enable it from the device settings. It is because some other restrictions might be active.
- Authorized: This is the state when the user has allowed the application to have access of the device contacts database. And the app can perform further functionalities accordingly.
For Contacts Framework
For Addressbook framework
The fundamental classes that we will be using while working with contacts is the CNContactStore in Contacts Framework and AddressBook in Address Framework.
These classes represent the device contact database. They are the ones which coordinate between the application and the actual database.
They perform the following two functions :
- Fetching Contacts from the device database.
- Saving the contacts to the device or updating the already existing contacts.
For using Contacts Framework you have to import the framework as mentioned below:
Fetching Contacts for iOS 9.0: If you are supporting versions below iOS 9.0, you will have to check for the iOS 9.0 availability.
So, this is how we fetch phone contacts using Contacts framework. This is a lot easier as compared to the Addressbook framework. Wondering why? Because it’s a very complex and lengthy code and it will consume the scope of this blog. Read how to fetch contacts using Addressbook framework here.
Creating a new contact and saving it:
So far, we have discussed how we prompt the user for the Authorization, how to fetch contacts from the device. However, there is still one thing that needs to be discussed, that is, how to create a new contact programmatically and store it to the database. We will demonstrate this by using both the frameworks.
Using Contacts Framework:
For Contacts Framework we need CNMutableContact which is similar to CNContact. It also represents a single contact and all of its properties.
However, CNMutableContact allows to assign new values to the properties of a contact, and therefore create a new one or update an existing contact.
We will have to import ContactsUI framework to our class.
The below function is for a creating a new contact and saving to the device contact Database.
We will have to conform our class, where we are writing our save contact logic, to the CNContactViewControllerDelegate protocol and then implement its function in our code. Below is the function :
This delegate is called when the contact we created is saved. Hence we can dismiss the CNContactViewController which we presented.
If you are supporting iOS 8.0 in your app then you will have to do this by using Addressbook Framework also.
So, now we will show how to create and save contacts using Addressbook Framework for iOS 8.0.
Using AddressBook Framework:
First, we will have to import AddressBookUI framework to our class.
We will have to conform our class, where we are writing the save contact logic, to the ABNewPersonViewControllerDelegate protocol and then implement its function in our code. Below is the function :
So we discussed how to ask user for contacts permissions, fetch contacts from the device and save the new contacts to the device.
We found that there is a substantial change between the two frameworks. We can see that with the introduction to Contacts framework, how easy it has become to work with contacts.
I hope I was able to make a point and show why Contacts framework is better than AddressBook framework. I wish you enjoyed reading this and I hope you will find it useful.
Have great learning days ahead!
This post first appeared on C.R.A.P. Design Principles To Improve Visual Appeal, please read the originial post: here