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!
Let me introduce you our 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.
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.
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
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 ;)
This article is cross-posted with my my personal blog.