A simple domain-oriented library to handle phone numbers! Compatible with all platforms where Swift run (see Compatibility section for more information).
Goal of the library is to be as simple to use as possible AND easy to add new functionalities such as Countries.
Contribution to complete missing countries is appreciated.
SwiftPhoneNumber allow you to build only valid numbers:
import SwiftPhoneNumber
do {
let phoneNumber = try PhoneNumber(number: "+33102030405")
// OR
let phoneNumber = try PhoneNumber(number: "+33102030405", for: [.fra, .usa, ...])
}
catch {
}
Once you have it, do whatever you want with it.
SwiftPhoneNumber also provide a formatter for UITextField
:
import SwiftPhoneNumber
import SwiftPhoneNumber_UIKit
class ViewController: UIViewController {
@IBOutlet var textField: UITextField!
let delegate = TextFieldPhoneNumberDelegate()
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = delegate
}
}
You can then get current input object directly from the delegate:
// nil if no input, an error if number is not valid otherwise a `PhoneNumber` object
delegate.value
try it out running our example SwiftPhoneNumberExample
SwiftPhoneNumber can be installed throughout Swift Package Manager:
let package = Package(
name: "YourAwesomeProject",
products: [],
dependencies: [
// remember to change version number to use latest available
.package(url: "https://github.com/pjechris/SwiftPhoneNumber", from: "0.0"),
],
targets: [
name: "YourTarget",
dependencies: [
"SwiftPhoneNumber",
.product("SwiftPhoneNumber-UIKit", package: "SwiftPhoneNumber") // <- add this only if you want to have UITextField support
]
]
SwiftPhoneNumber should be able to run on any platform where Swift. That being said it has only been tested on iOS. If you find an incompatibility issue, pleas fill a bug.
See Countries
To add a new country:
Country.swift
. You'll have to provide info such name, flag emoji, iso codes.Country+Data
PhoneNumberFormatter
is used to format a country. To create your own, you just have to make a new instance and pass 2 format style:
If both are the same you can just pass one argument.
// Available interpolations are:
// - .code to international/nationalcode
// - a Int number that will take Int characters from subscriber number
let franceFormatter = PhoneNumberFormatter("\(.code)\(1) \(2) \(2) \(2) \(2)")
With a french number "0102030405" it would give "01 02 03 04 05"
Something is wrong with our TextFieldPhoneNumberDelegate
? Or maybe you want something more? Under the hood TextFieldPhoneNumberDelegate
is just a TextFieldFormatterDelegate
with a InputFormatter
to define formatting rules:
PhoneNumber
PhoneNumberFormatter
)So just define your own if you need to.
This project is released under the MIT License. Please see the LICENSE file for details.
link |
Stars: 1 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics