Giter Site home page Giter Site logo

prasanthu / phonenumberkit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marmelroy/phonenumberkit

0.0 1.0 0.0 1.58 MB

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

License: MIT License

Swift 97.27% Ruby 1.56% Objective-C 0.37% Shell 0.79%

phonenumberkit's Introduction

PhoneNumberKit Platform Build Status Version Carthage compatible

PhoneNumberKit

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

Migrating from PhoneNumberKit 0.x? See the migration guide.

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

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 dialing code for a given country

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

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', '~> 2.1'

phonenumberkit's People

Contributors

marmelroy avatar davbeck avatar dr-skot avatar artyom-stv avatar dulacp avatar redcapua avatar codytwinton avatar egrim avatar jwolkovitz avatar phatmann avatar dmcgloin avatar coledunsby avatar dpopov avatar danielrhodes avatar sweetmandm avatar jshier avatar kostiakoval avatar leomehlig avatar sorth-hiya avatar vhbit avatar appsforall avatar hongxinhope avatar nrako avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.