libPhoneNumberSwift 0.0.11

libPhoneNumberSwift 0.0.11

疯狂凡维护。



  • 作者
  • kongkaikai

libPhoneNumberSwift


libPhoneNumber for iOS (swift)

  • PhoneNumberUtil
  • AsYouTypeFormatter

是从libphonenumberlibPhoneNumber-iOS(谷歌的电话号码处理库用于iOS、TVOS、WatchOS、MacOS的Objective-C实现)移植过来的iOS版本。

更新日志

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

问题

您可以使用以下链接检查电话号码验证。https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html

如果以上结果与此iOS库的结果不同,请报告。否则,请创建以下链接的问题来请求更多的电话号码格式化规则。https://github.com/google/libphonenumber/issues

此库中的元数据是从那个生成的,因此您应该先更改它。 :)

安装

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

说明

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

输出

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

输出

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

作者

crazy凡, [email protected]

许可证

libPhoneNumberSwift遵循Apache许可证发布。有关详细信息,请参阅LICENSE