Prefill an signup form in iOS using Swift and CNContact

Usability and user experience is key to a successful app, so narrowing the taps a user needs to do to get from point a to point b is crucial. One of the areas where a users often needs to make a lot of taps is on signup forms; this, however, has been tackled different ways by many apps. Today the use of social login, before apps used the address book (e.g. the square app).

The last option is the one I am going to explain here. Since square had this option in their app, things have changed in the iOS platform. Nowadays, Apple doesn’t allow to get data from the address book before asking permission from the user; besides that, neither the old ABAddressBook nor the new CNContact have a “me” option, as there is in the OSX SDK. To solve this, you need to make an educated guess and use that guess to pre-fill the fields. This is what we are going to do here.

Asking the user to use the Address book

permission

Just calling the CNContact will result in a question from the OS on wether the app is allowed to access the contact list. Only this is very intuitive for the users, because he/she might not know or understand why you suddenly want to access the addressbook, so I suggest you only do this when the user explicitly asks for it themselves, thus they also understand the request.

The idea

The idea is to use the device name, why? Because the first time you start your iDevice, you will name it. Apple will prefill the device name using your name which you entered when registering for an Apple ID and the model name of the device. For example, in my case this would result in “Paul Peelen’s iPhone 6s+”. For different countries, this might be formatted differently though. For instance, in swedish this would be “Paul Peelens iPhone 6s+” (no apostrophe on the name). Given we get this name, we could make an educated guess on whom the contact is. One thing we take for granted though, in the example I have written, is that the first result is the current user. However, you could have a fallback solution that if several results are found, you let the user choose which to use. Also, another fallback solution would be to show the full address book if the search doesn’t return any results.

The code

In order for this to work, you need to add the “Contact” framework to your app.

I have commented the code as well as I could, be I’ll explain it a bit further anyways. This is the code:

And a small extension:

getMe()

This function is responsible for accessing the adressbook and searching for the user. It will call getOwnerName to get the name to search for. Here you could also add a check for the owner name in, e.g. check if it is longer then 3 characters. You should also handle the case if no contacts are found (e.g. by opening a table with all the contacts, or using contactUI?)

getOwnerName()

This function will do the funky stuff of getting the name of the contact to search for. It will remove the device name from the device owner name and return that, trimmed.

populateUsingContact()

Well, I guess the method name says it all. It populates the fields using the CNContact item retuned. You need to remember here that not all information might be available; so if information is missing, you need to handle it.

Result

The result is that the fields are populated. Of course, you need to remember that this is not a final solution, since the contact might be wrong or might not exist. But, give it a go.

Contact result

The example/source

I’ve put it up on Github for you tinker around with!

https://github.com/ppeelen/PrefillUsingAddressBook

I hope this post was helpful for you; please let me know in the comments below!

Cheers,
Paul

Posted in Cocoa Touch, Development, Free stuff, iOS, iPhone, Swift, Tutorials Tagged with: , , , ,
One comment on “Prefill an signup form in iOS using Swift and CNContact
  1. curoda says:

    Thanks much for this great post to help avoid typing where possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

 

Categories