Fetching contacts with CNContactStore


Since iOS 9.0 we have a new nice method of fetching and saving contacts - CNContactStore! Today I will show you how to create a simple UITextField that will be responsible for fetching a contact for you!

Setting up

Let me introduce you our textfield!

Empty TextField

Our text field will be unique this time. It will not behave the way it usually does. After tapping it we don’t want to see a keyboard, we want to see contacts! In order to achieve this, first we have to implement specific delegate method from our textfield. Inside it, we will open our contact list and tell our textfield not to display a keyboard.

Display contacts

Now it’s time to present contacts, after creating our CNContactPickerViewController, we will assign a delegate for it (we will implement it’s method later to catch a contact) and tell it to display contact’s phone numbers only.

Contact list

Get a phone number

After we choose a phone number from a contact list, we want to receive it in our app. We will do it by catching CNContactProperty with CNContactPickerViewController's delegate method. You can find many other things when you dig into CNContactProperty like name, familyName etc.

Selected phone number will now appear in our text field ;)

Filled TextField

This article is cross-posted with my my personal blog.