All Projects → marmelroy → Phonenumberkit

marmelroy / Phonenumberkit

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

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Phonenumberkit

Intl Tel Input
A JavaScript plugin for entering and validating international telephone numbers
Stars: ✭ 5,963 (+36.7%)
Mutual labels:  validation, formatting, phone-number
intl-tel-input-rails
intl-tel-input for the Rails asset pipeline
Stars: ✭ 35 (-99.2%)
Mutual labels:  validation, phone-number, formatting
Firely Net Sdk
The official Firely .NET SDK for HL7 FHIR
Stars: ✭ 560 (-87.16%)
Mutual labels:  validation, parsing
Graphql Go Tools
Tools to write high performance GraphQL applications using Go/Golang.
Stars: ✭ 96 (-97.8%)
Mutual labels:  validation, parsing
csv2vcf
🔧 Simple script in python to convert CSV files to VCF
Stars: ✭ 66 (-98.49%)
Mutual labels:  phone-number, contacts
Pydantic
Data parsing and validation using Python type hints
Stars: ✭ 8,362 (+91.7%)
Mutual labels:  validation, parsing
Phonelib
Ruby gem for phone validation and formatting using google libphonenumber library data
Stars: ✭ 731 (-83.24%)
Mutual labels:  parsing, phone-number
React Native Unified Contacts
Your best friend when working with the latest and greatest Contacts Framework in iOS 9+ in React Native.
Stars: ✭ 156 (-96.42%)
Mutual labels:  phone-number, contacts
dockerfile-utils
A library and command line interface for formatting and linting Dockerfiles.
Stars: ✭ 17 (-99.61%)
Mutual labels:  validation, formatting
CHRTextFieldFormatter
Provides UITextField formatting masks. Such as phone number and credit card number formatters.
Stars: ✭ 52 (-98.81%)
Mutual labels:  phone-number, formatting
international-telephone-input
Integration to Magento 2 a jQuery plugin for entering and validating international telephone numbers.
Stars: ✭ 26 (-99.4%)
Mutual labels:  phone-number, formatting
fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (-99.22%)
Mutual labels:  validation, parsing
webargs-starlette
Declarative request parsing and validation for Starlette with webargs
Stars: ✭ 36 (-99.17%)
Mutual labels:  validation, parsing
apple-receipt
Apple InAppPurchase Receipt - Models, Parser, Validator
Stars: ✭ 25 (-99.43%)
Mutual labels:  validation, parsing
Jsoniter Scala
Scala macros for compile-time generation of safe and ultra-fast JSON codecs
Stars: ✭ 410 (-90.6%)
Mutual labels:  parsing
Phoneinfoga
PhoneInfoga is one of the most advanced tools to scan international phone numbers using only free resources. It allows you to first gather standard information such as country, area, carrier and line type on any international phone number. Then search for footprints on search engines to try to find the VoIP provider or identify the owner.
Stars: ✭ 5,927 (+35.88%)
Mutual labels:  phone-number
Colander
A serialization/deserialization/validation library for strings, mappings and lists.
Stars: ✭ 408 (-90.65%)
Mutual labels:  validation
Vue Phone Number Input
A phone number input made with Vue JS (format & valid phone number)
Stars: ✭ 407 (-90.67%)
Mutual labels:  phone-number
Android Multipicker Library
Android Multipicker Library
Stars: ✭ 425 (-90.26%)
Mutual labels:  contacts
Ascii Tables
Quickly format table in ASCII. Great for code comments, or Github Markdown!
Stars: ✭ 416 (-90.46%)
Mutual labels:  formatting

PhoneNumberKit Platform Build Status Version Carthage compatible

PhoneNumberKit

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

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

All of your interactions with PhoneNumberKit happen through a PhoneNumberKit object. The first step you should take is to allocate one.

A PhoneNumberKit instance is relatively expensive to allocate (it parses the metadata and keeps it in memory for the object's lifecycle), you should try and make sure PhoneNumberKit is allocated once and deallocated when no longer needed.

let phoneNumberKit = PhoneNumberKit()

To parse a string, use the parse function. The region code is automatically computed but can be overridden if needed. PhoneNumberKit automatically does a hard type validation to ensure that the object created is valid, this can be quite costly performance-wise and can be turned off if needed.

do {
    let phoneNumber = try phoneNumberKit.parse("+33 6 89 017383")
    let phoneNumberCustomDefaultRegion = try phoneNumberKit.parse("+44 20 7031 3000", withRegion: "GB", ignoreType: true)
}
catch {
    print("Generic parser error")
}

If you need to parse and validate a large amount of numbers at once, PhoneNumberKit has a special, lightning fast array parsing function. The default region code is automatically computed but can be overridden if needed. Here you can also ignore hard type validation if it is not necessary. Invalid numbers are ignored in the resulting array.

let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
let phoneNumbers = phoneNumberKit.parse(rawNumberArray)
let phoneNumbersCustomDefaultRegion = phoneNumberKit.parse(rawNumberArray, withRegion: "DE",  ignoreType: true)

PhoneNumber objects are immutable Swift structs with the following properties:

phoneNumber.numberString
phoneNumber.countryCode
phoneNumber.nationalNumber
phoneNumber.numberExtension
phoneNumber.type // e.g Mobile or Fixed

Formatting a PhoneNumber object into a string is also very easy

phoneNumberKit.format(phoneNumber, toType: .e164) // +61236618300
phoneNumberKit.format(phoneNumber, toType: .international) // +61 2 3661 8300
phoneNumberKit.format(phoneNumber, toType: .national) // (02) 3661 8300

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

[Preferrred] 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" with "3.3.3".
  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", .upToNextMajor(from: "3.3.3"))
]

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', '~> 3.3'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].