XUtils

PhoneNumberKit

Framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.


PhoneNumberKit Platform GitHub Workflow Status (with branch) Version Carthage compatible

PhoneNumberKit

Swift 5.3 framework for parsing, formatting and validating international phone numbers. Inspired by Google’s libphonenumber.

Features

Features
:phone: Validate, normalize and extract the elements of any phone number string.
:100: Simple Swift syntax and a lightweight readable codebase.
:checkered_flag: Fast. 1000 parses -> ~0.4 seconds.
:books: Best-in-class metadata from Google’s libPhoneNumber project.
:trophy: Fully tested to match the accuracy of Google’s JavaScript implementation of libPhoneNumber.
:iphone: Built for iOS. Automatically grabs the default region code from the phone.
📝 Editable (!) AsYouType formatter for UITextField.
:us: Convert country codes to country names and vice versa

PhoneNumberTextField

AsYouTypeFormatter

To use the AsYouTypeFormatter, just replace your UITextField with a PhoneNumberTextField (if you are using Interface Builder make sure the module field is set to PhoneNumberKit).

You can customize your TextField UI in the following ways

  • withFlag will display the country code for the currentRegion. The flagButton is displayed in the leftView of the text field with it’s size set based off your text size.
  • withExamplePlaceholder uses attributedPlaceholder to show an example number for the currentRegion. In addition when withPrefix is set, the country code’s prefix will automatically be inserted and removed when editing changes.

PhoneNumberTextField automatically formats phone numbers and gives the user full editing capabilities. If you want to customize you can use the PartialFormatter directly. The default region code is automatically computed but can be overridden if needed (see the example given below).

class MyGBTextField: PhoneNumberTextField {
    override var defaultRegion: String {
        get {
            return "GB"
        }
        set {} // exists for backward compatibility
    }
}
let textField = PhoneNumberTextField()

PartialFormatter().formatPartial("+336895555") // +33 6 89 55 55

You can also query countries for a dialing code or the dialing code for a given country

phoneNumberKit.countries(withCode: 33)
phoneNumberKit.countryCode(for: "FR")

Need more customization?

You can access the metadata powering PhoneNumberKit yourself, this enables you to program any behaviours as they may be implemented in PhoneNumberKit itself. It does mean you are exposed to the less polished interface of the underlying file format. If you program something you find useful please push it upstream!

phoneNumberKit.metadata(for: "AU")?.mobile?.exampleNumber // 412345678

[Preferred] Setting up with Swift Package Manager

The Swift Package Manager is now the preferred tool for distributing PhoneNumberKit.

From Xcode 11+ :

  1. Select File > Swift Packages > Add Package Dependency. Enter https://github.com/marmelroy/PhoneNumberKit.git in the “Choose Package Repository” dialog.
  2. In the next page, specify the version resolving rule as “Up to Next Major” from “3.7.0”.
  3. After Xcode checked out the source and resolving the version, you can choose the “PhoneNumberKit” library and add it to your app target.

For more info, read Adding Package Dependencies to Your App from Apple.

Alternatively, you can also add PhoneNumberKit to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/marmelroy/PhoneNumberKit", from: "3.7.0")
]

Setting up with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate PhoneNumberKit into your Xcode project using Carthage, specify it in your Cartfile:

github "marmelroy/PhoneNumberKit"

Setting up with CocoaPods

pod 'PhoneNumberKit', '~> 3.7'

Articles

  • coming soon...