MSSPhoneNumber is a Swift framework designed for international phone number validation with built-in support for selecting country codes.
- Display a country picker to allow users to select their country.
- Validate phone numbers to check if they are in a valid format.
- Check input text in a text field with respect to specified ranges and text.
- Retrieve country information by providing a country code.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
To add MSSPhoneNumber as a dependency to your Swift package, follow these steps:
- In your
Package.swiftfile, add the following line to thedependenciessection:
dependencies: [
.package(url: "https://github.com/skaik-mo/MSSPhoneNumber.git", from: "1.0.0")
]- To install MSSPhoneNumber via Xcode, do the following:
- Go to File -> Add Package Dependency...
- Search for https://github.com/skaik-mo/MSSPhoneNumber.git
- Choose the version you want
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate MSSPhoneNumber into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'MSSPhoneNumber'Import MSSPhoneNumber at the top of the Swift file that will interact with a phone number.
import MSSPhoneNumberAll interactions with MSSPhoneNumber happen through a MSSPhoneNumber object. To get started, allocate one:
let mssPhoneNumber = MSSPhoneNumber()You can use MSSPhoneNumber to retrieve country information by providing a country code:
let country: Country? = self.mssPhoneNumber.getCountry(code: "PS")The showCountryPicker() function is responsible for displaying a country picker view controller:
mssPhoneNumber.getSelectedCountry = { country in
// Handle the selected country in this completion handler
}
mssPhoneNumber.showCountryPicker()The isValid(fieldTitle:phone:) function is used to validate a phone number string. It takes two parameters:
fieldTitle: A string representing the title of the field being validated (default value is "Phone number").
phone: The phone number string to be validated.
let isValid: Bool = mssPhoneNumber.isValid(fieldTitle: "Phone number", phone: "123456789")The checkTextInput(textField:range:text:) function is used to check if adding or replacing text in a UITextField would result in a valid text based on certain criteria. You can integrate it into your UITextFieldDelegate methods:
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return mssPhoneNumber.checkRangText(textField: textField, range: range, text: string)
}
}This function helps ensure that text input adheres to specific criteria, which can be particularly useful for phone number entry.
Mohammed Skaik
- Email: mohamedsaeb.skaik@gmail.com
- Linkedin: skaik-mo
MSSPhoneNumber is released under the MIT license. See LICENSE for details.