jakunico / PhoneNumberKit

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PhoneNumberKit

Build Status Version Carthage compatible

PhoneNumberKit

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

🚧 PhoneNumberKit is currently beta software 🚧

| Remaining Objectives --- | --- βš” | Battle-test PhoneNumberKit in a major app (100k+ users).

Features

          |  Features

--------------------------|------------------------------------------------------------ ☎️ | Validate, normalize and extract the elements of any phone number string. πŸ’― | Simple Swift syntax and a lightweight readable codebase. 🏁 | Fast. 1000 parses -> ~0.3 seconds. πŸ“š | Best-in-class metadata from Google's libPhoneNumber project. πŸ† | Fully tested to match the accuracy of Google's JavaScript implementation of libPhoneNumber. πŸ“± | Built for iOS. Automatically grabs the default region code from the phone. πŸ“ | Editable (!) AsYouType formatter for UITextField. πŸ‡ΊπŸ‡Έ | Convert country codes to country names and vice versa

Usage

Import PhoneNumberKit at the top of the Swift file that will interact with a phone number.

import PhoneNumberKit

To parse and validate a string, initialize a PhoneNumber object and supply the string as the rawNumber. The region code is automatically computed but can be overridden if needed. In case of an error, it will throw and you can catch and respond to in your app's UI

do {
    let phoneNumber = try PhoneNumber(rawNumber:"+33 6 89 017383")
    let phoneNumberCustomDefaultRegion = try PhoneNumber(rawNumber: "+44 20 7031 3000", region: "GB")
}
catch {
    print("Generic parser error")
}

If you need to parse and validate a large amount of numbers at once, there is a special function for that and it's lightning fast. The default region code is automatically computed but can be overridden if needed.

let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
let phoneNumbers = PhoneNumberKit().parseMultiple(rawNumberArray)
let phoneNumbersCustomDefaultRegion = PhoneNumberKit().parseMultiple(rawNumberArray, region: "DE")

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).

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.

AsYouTypeFormatter

let textField = PhoneNumberTextField()

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

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

let phoneNumberKit = PhoneNumberKit()
phoneNumberKit.countriesForCode(33)
phoneNumberKit.codeForCountry("FR")

Formatting a parsed phone number to a string is also very easy

phoneNumber.toE164() // +61236618300
phoneNumber.toInternational() // +61 2 3661 8300
phoneNumber.toNational() // (02) 3661 8300

You can access the following properties of a PhoneNumber object

phoneNumber.countryCode
phoneNumber.nationalNumber
phoneNumber.numberExtension
phoneNumber.rawNumber
phoneNumber.type // e.g Mobile or Fixed
phoneNumber.isValidNumber // Checks if number has a known type

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

source 'https://github.com/CocoaPods/Specs.git'
pod 'PhoneNumberKit', '~> 0.8'

About

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

License:MIT License


Languages

Language:Swift 97.5%Language:Ruby 1.2%Language:Shell 0.8%Language:Objective-C 0.4%