You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
5.2 KiB

5 years ago
  1. ![PhoneNumberKit](https://cloud.githubusercontent.com/assets/889949/20864386/a1307950-b9ef-11e6-8a58-e9c5103738e7.png)
  2. [![Platform](https://img.shields.io/cocoapods/p/PhoneNumberKit.svg?maxAge=2592000)](http://cocoapods.org/?q=PhoneNumberKit)
  3. [![Build Status](https://travis-ci.org/marmelroy/PhoneNumberKit.svg?branch=master)](https://travis-ci.org/marmelroy/PhoneNumberKit) [![Version](http://img.shields.io/cocoapods/v/PhoneNumberKit.svg)](http://cocoapods.org/?q=PhoneNumberKit)
  4. [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
  5. # PhoneNumberKit
  6. Swift 4.2 framework for parsing, formatting and validating international phone numbers.
  7. Inspired by Google's libphonenumber.
  8. [Migrating from PhoneNumberKit 0.x? See the migration guide.](https://github.com/marmelroy/PhoneNumberKit/blob/master/Documentation/OXMIGRATIONGUIDE.md)
  9. ## Features
  10. | |Features |
  11. --------------------------|------------------------------------------------------------
  12. :phone: | Validate, normalize and extract the elements of any phone number string.
  13. :100: | Simple Swift syntax and a lightweight readable codebase.
  14. :checkered_flag: | Fast. 1000 parses -> ~0.4 seconds.
  15. :books: | Best-in-class metadata from Google's libPhoneNumber project.
  16. :trophy: | Fully tested to match the accuracy of Google's JavaScript implementation of libPhoneNumber.
  17. :iphone: | Built for iOS. Automatically grabs the default region code from the phone.
  18. 📝 | Editable (!) AsYouType formatter for UITextField.
  19. :us: | Convert country codes to country names and vice versa
  20. ## Usage
  21. Import PhoneNumberKit at the top of the Swift file that will interact with a phone number.
  22. ```swift
  23. import PhoneNumberKit
  24. ```
  25. All of your interactions with PhoneNumberKit happen through a PhoneNumberKit object. The first step you should take is to allocate one.
  26. 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.
  27. ```swift
  28. let phoneNumberKit = PhoneNumberKit()
  29. ```
  30. 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.
  31. ```swift
  32. do {
  33. let phoneNumber = try phoneNumberKit.parse("+33 6 89 017383")
  34. let phoneNumberCustomDefaultRegion = try phoneNumberKit.parse("+44 20 7031 3000", withRegion: "GB", ignoreType: true)
  35. }
  36. catch {
  37. print("Generic parser error")
  38. }
  39. ```
  40. 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.
  41. ```swift
  42. let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
  43. let phoneNumbers = phoneNumberKit.parse(rawNumberArray)
  44. let phoneNumbersCustomDefaultRegion = phoneNumberKit.parse(rawNumberArray, withRegion: "DE", ignoreType: true)
  45. ```
  46. PhoneNumber objects are immutable Swift structs with the following properties:
  47. ```swift
  48. phoneNumber.numberString
  49. phoneNumber.countryCode
  50. phoneNumber.nationalNumber
  51. phoneNumber.numberExtension
  52. phoneNumber.type // e.g Mobile or Fixed
  53. ```
  54. Formatting a PhoneNumber object into a string is also very easy
  55. ```swift
  56. phoneNumberKit.format(phoneNumber, toType: .e164) // +61236618300
  57. phoneNumberKit.format(phoneNumber, toType: .international) // +61 2 3661 8300
  58. phoneNumberKit.format(phoneNumber, toType: .national) // (02) 3661 8300
  59. ```
  60. 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).
  61. 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.
  62. ![AsYouTypeFormatter](http://i.giphy.com/3o6gbgrudyCM8Ak6yc.gif)
  63. ```swift
  64. let textField = PhoneNumberTextField()
  65. PartialFormatter().formatPartial("+336895555") // +33 6 89 55 55
  66. ```
  67. You can also query countries for a dialing code or the dialing code for a given country
  68. ```swift
  69. phoneNumberKit.countries(withCode: 33)
  70. phoneNumberKit.countryCode(for: "FR")
  71. ```
  72. ### Setting up with Carthage
  73. [Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
  74. You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
  75. ```bash
  76. $ brew update
  77. $ brew install carthage
  78. ```
  79. To integrate PhoneNumberKit into your Xcode project using Carthage, specify it in your `Cartfile`:
  80. ```ogdl
  81. github "marmelroy/PhoneNumberKit"
  82. ```
  83. ### Setting up with [CocoaPods](http://cocoapods.org/?q=PhoneNumberKit)
  84. ```ruby
  85. source 'https://github.com/CocoaPods/Specs.git'
  86. pod 'PhoneNumberKit', '~> 2.5'
  87. ```