libPhoneNumberSwift

iOS library for parsing, formatting, storing and validating international phone numbers from libphonenumber library.


Licenses
Apache-2.0/CERN-OHL-S-2.0/MIT-feh
Install
pod try libPhoneNumberSwift

Documentation

libPhoneNumberSwift


libPhoneNumber for iOS (swift)

  • PhoneNumberUtil
  • AsYouTypeFormatter

iOS port from libphonenumberlibPhoneNumber-iOS(Google's phone number handling library & Objective-C implementation for iOS\TVOS\WatchOS\MacOS)

Update-Log

https://github.com/CrazyFanFan/libPhoneNumberSwift/wiki/Update-Log

Issue

You can check phone number validation using below link. https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html

Please report, if the above results are different from this iOS library. Otherwise, please create issue to following link below to request additional telephone numbers formatting rule. https://github.com/google/libphonenumber/issues

Metadata in this library was generated from that. so, you should change it first. :)

Installation

source 'https://github.com/CocoaPods/Specs.git'
pod 'libPhoneNumberSwift'

Instructions

PhoneNumberUtil
import libPhoneNumberSwift

func testPhoneNumberUtil() {
    let util = PhoneNumberUtil.instance
    do {
        let number = try util.parse("6766077303", defaultRegion: "AT")
        NSLog("isValidPhoneNumber ? [\(util.isValidNumber(number))]")

        // E164          : +436766077303
        NSLog("E164          : \(util.format(number, numberFormat: .e164))")

        // INTERNATIONAL : +43 676 6077303
        NSLog("INTERNATIONAL : \(util.format(number, numberFormat: .international))")

        // NATIONAL      : 0676 6077303
        NSLog("INTERNATIONAL : \(util.format(number, numberFormat: .national))")

        // RFC3966       : tel:+43-676-6077303
        NSLog("INTERNATIONAL : \(util.format(number, numberFormat: .rfc3966))")
    } catch let error {
        NSLog((error as? PhoneNumberError)?.message ?? error.localizedDescription)
    }

    NSLog("extractCountryCode [\(util.extractCountryCode("823213123123"))]")

    var nationalNumber = ""
    let countryCode = util.extractCountryCode("823213123123", nationalNumber: &nationalNumber)
    NSLog ("extractCountryCode [\(countryCode)] [\(nationalNumber)]");
}
testPhoneNumberUtil()

Output

2019-08-03 00:36:28.620086+0800 PhoneNumberSwiftDemo[9891:1449888] isValidPhoneNumber ? [true]
2019-08-03 00:36:28.620302+0800 PhoneNumberSwiftDemo[9891:1449888] E164          : +436766077303
2019-08-03 00:36:28.620860+0800 PhoneNumberSwiftDemo[9891:1449888] INTERNATIONAL : +43 676 6077303
2019-08-03 00:36:28.621183+0800 PhoneNumberSwiftDemo[9891:1449888] INTERNATIONAL : 0676 6077303
2019-08-03 00:36:28.621472+0800 PhoneNumberSwiftDemo[9891:1449888] INTERNATIONAL : tel:+43-676-6077303
2019-08-03 00:36:28.621935+0800 PhoneNumberSwiftDemo[9891:1449888] extractCountryCode [82]
2019-08-03 00:36:28.622061+0800 PhoneNumberSwiftDemo[9891:1449888] extractCountryCode [82] [3213123123]
AsYouTypeFormatter
import libPhoneNumberSwift

func testAsYouTypeFormatter() {
    let f = AsYouTypeFormatter(regionCode: "US")
    NSLog(f.inputDigit("6"))
    NSLog(f.inputDigit("5"))
    NSLog(f.inputDigit("0"))
    NSLog(f.inputDigit("2"))
    NSLog(f.inputDigit("5"))
    NSLog(f.inputDigit("3"))

    // Note this is how a US local number (without area code) should be formatted.
    NSLog(f.inputDigit("2"))
    NSLog(f.inputDigit("2"))
    NSLog(f.inputDigit("2"))
    NSLog(f.inputDigit("2"))
    // Can remove last digit
    NSLog(f.removeLastDigit())

    NSLog(f.inputString("16502532222")) // 1 650 253 2222
}
testAsYouTypeFormatter()

Output

2019-08-03 00:44:51.943330+0800 PhoneNumberSwiftDemo[10295:1485208] 6
2019-08-03 00:44:51.943545+0800 PhoneNumberSwiftDemo[10295:1485208] 65
2019-08-03 00:44:51.945661+0800 PhoneNumberSwiftDemo[10295:1485208] 650
2019-08-03 00:44:51.945898+0800 PhoneNumberSwiftDemo[10295:1485208] 650-2
2019-08-03 00:44:51.946171+0800 PhoneNumberSwiftDemo[10295:1485208] 650-25
2019-08-03 00:44:51.946441+0800 PhoneNumberSwiftDemo[10295:1485208] 650-253
2019-08-03 00:44:51.946648+0800 PhoneNumberSwiftDemo[10295:1485208] 650-2532
2019-08-03 00:44:51.947586+0800 PhoneNumberSwiftDemo[10295:1485208] (650) 253-22
2019-08-03 00:44:51.947985+0800 PhoneNumberSwiftDemo[10295:1485208] (650) 253-222
2019-08-03 00:44:51.948183+0800 PhoneNumberSwiftDemo[10295:1485208] (650) 253-2222
2019-08-03 00:44:51.949922+0800 PhoneNumberSwiftDemo[10295:1485208] (650) 253-222
2019-08-03 00:44:51.951272+0800 PhoneNumberSwiftDemo[10295:1485208] 1 650-253-2222

Author

crazy凡, ccrazyfan@gmail.com

License

libPhoneNumberSwift is released under the Apache license. See LICENSE for details.