Code Monkey home page Code Monkey logo

phonenumberkit's Introduction

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 alpha software ๐Ÿšง

| Objective --- | --- :white_check_mark: | Match Google's libphonenumber accuracy by passing tests against all example numbers :white_check_mark: | Faster performance. 1000 parses -> ~0.3 seconds. :x: | Better formatter and an AsYouType formatter for UITextField

Features

  • Quickly validate, normalize and extract the elements of any phone number string.
  • Special function to quickly parse a large array of raw phone numbers.
  • Automatically grab the default region code from the phone's SIM (or if unavailable, the device's region). You can override this if you need to.
  • Convert country codes to country names and vice versa.
  • Simple Swift 2.0 syntax and readable codebase.
  • PhoneNumberKit uses the best-in-class metadata and basic approach from Google's libphonenumber project. By not being a direct port, PhoneNumberKit can focus on a smaller feature-set that's cleaner, faster and more readable.

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 phoneNumberForCustomDefaultRegion = 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 phoneNumbersForCustomDefaultRegion = PhoneNumberKit().parseMultiple(rawNumberArray, region: "DE")

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

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

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.1'

phonenumberkit's People

Contributors

marmelroy avatar kostiakoval avatar

Watchers

 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.