Working With Phone Numbers in Swift
A month ago, I released my first open source project — Localise-Swift, a better way to deal with international strings in Swift. For my second open source project, I wanted to stay in the realm of i18n but tackle a bigger problem — the parsing, validating and formatting of phone numbers.
Well… it turns out there is a direct port of libPhoneNumber for iOS - a very impressive work, mostly by a developer called iziz. I’ve used it in many apps and contributed a little bit to the project too. However, there are downsides to the direct port approach — mainly, libphonenumber’s source code is huge and the code itself is very obscure. When you untangle libphonenumber you realise a lot of this complexity is unnecessary on iOS and much of it is there to support functionality that makes more sense on web environments. Oh, and it’s all in Objective C. What would a Swift libPhoneNumber look like?
PhoneNumberKit is a new Swift dynamic framework for parsing, validating and formatting phone numbers. It is inspired by libphonenumber, taking the general approach and metadata from Google’s project but it is not a direct port. It aims to support a smaller feature-set with a graceful, simple and Swift-y approach.
- Simple parsing and automatic validation of any phone number with minimum set up.
- All while using best-in-class metadata and regular expressions from libphonenumber.
- Lightweight, clean and easy to maintain Swift 2.0 code.
- Automatically recognise the user’s default region from their SIM card or, if unavailable, the device’s region settings.