Parsing Phone Numbers With libPhoneNumber-iOS

In this blog post we'll go over setting up your project and we'll get started parsing phone numbers with libPhoneNumber-iOS.libPhoneNumber-iOS is an iOS port of the Google’s libPhoneNumber library. It’s very simple to use and with cocoapods it’s real easy to get started with it. In this blog post we’ll go over setting up your project and we’ll get started parsing phone numbers with libPhoneNumber-iOS.

The Library

Like we already mentioned, libPhoneNumber is a library developed by Google and libPhoneNumber-iOS is the iOS port of the library. Apart from parsing and formatting the numbers, the library can do a bunch of other stuff too 🙂 For example, you can easily extract the country, you can parse a text string and extract a number out of it, or even find out if the phone number is toll-free. One other cool thing you can do with this library is parse the characters into a dial-able numbers, for example, 555-FILM would parse to 555-3456. Let’s check some of these cool features in action.

The Project

The simplest way to install this library is by using cocoapods, so go ahead and add the pod to your podfile and run pod install.

We won’t create a fancy project. We’ll only parse a couple of strings into a phone number to see some of the features the library has to offer. We’ll also have a simple UI that will parse a phone number as you type. This is something that you might use on your forms as a feedback to your users.

This will be our complex 🙂 UI:

Let’s write some code.

The Code

We’ll put all the code in our view controller. Let’s start with a simple parser. We’ll assume you want to parse US phone numbers. So, parsing a US phone number is as simple as:

The console will print out the number in the international number format:

If you’re not feeling creative, you even have a function that will give you an example number:

Now, to be perfectly fair, this is meant to be used as a help to your users when they’re inputting a phone number. You’ve probably seen forms that have this already. All you have to do is get the current region and pass it in as a parameter.

A more interesting feature is full text parsing. The library can parse a blob of text and extract a phone number from it:

This function will return a string that will start with a phone number. It won’t return just a phone number, it will also return everything after the number as well. That’s why we have to normalise it before extracting the number. This is a very handy function. Imagine you’re building a chat app and you want to highlight all the phone numbers in the chat and call them if the user taps on any.

You know how you sometimes see a phone number in a format like ‘555-FILM’. Most phone keyboards have numbers on them, so you just type in the number and the ‘word’ using the keyboard, and you dial the number. It’s supposed to help people remember the numbers better. I don’t know if that’s true or not, but, this little library can convert this number into a proper phone number that you can dial. It’s quite simple, really:

This will print in the console:

Format As You Type

Another useful feature of this library is the ability to format the phone numbers as your user is typing the number. Technically, you’re appending numbers to the formatter 🙂 We have our simple app. And all we have to do is implement the delegate callback of our textfield and start adding the numbers to the formatter:

Yes, you would build a more robust code for production with some validators, but for the purposes of demoing the library this will do just fine 🙂

Here’s how it looks in action:

 

These are just some of the features that the library has. But, they should be enough to get you started and they’ll probably cover most use cases for you as well. Feel free to poke around the library and see what else it can do for you 🙂

Conclusion

This nice little library is perfect for forms, for validation and for displaying hints to your users. It has that great feature where it can parse a phone number out of any blob of text. You can do a bunch of other useful stuff with it too. For example, know from which country a certain number is. Or if it’s a toll-free number. All in all, a very useful library if you need to parse phone numbers.

As usual, you can find all the code on GitLab and I hope you’ve discovered something new today.

Have a nice day 🙂
~D;

More resources

3 thoughts on “Parsing Phone Numbers With libPhoneNumber-iOS

  1. michaelwaung

    Very nice blog, Thanks for sharing such valuable information. I will share this information with my friends.
    This information is so effective for user. Hope you keep sharing such kind of information

    Reply
  2. Derek

    Hi!
    In the last snipper of code you don’t specify which formatter is used for realtime typing in textfield

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.