Introduction
Welcome to InputKit, a comprehensive library for handling input validation, formatting, and masking in iOS applications. With InputKit, developers can easily validate user input, apply specific formatting rules, and mask sensitive data such as credit card numbers or phone numbers to enhance app user experience.
Installation
To integrate InputKit into your iOS project, you can use either CocoaPods or Swift Package Manager.
CocoaPods
- Create a Podfile in your project’s root directory if you don’t have one.
- Add the following line to your Podfile:
“`ruby
pod ‘InputKit’
“`
- Save the Podfile and run the command:
“`bash
pod install
“`
Swift Package Manager
- In Xcode, open your project.
- Select “File” -> “Swift Packages” -> “Add Package Dependency”
- Enter the InputKit repository URL:
“`
https://github.com/InputKit/InputKit.git
“`
- Choose the version you want to use and click “Next” to add the package to your project.
Usage
Basic Validation
InputKit provides several ready-to-use validation rules for common use cases, such as email validation, password strength validation, and more.
Here’s an example of how to validate an email address:
“`swift
import InputKit
let emailTextField = UITextField()
emailTextField.validationRules = ValidationRuleSet(rules: [.email])
emailTextField.validateOnInputChange(enabled: true)
“`
Formatting Input
If you need to apply specific formatting to user input, InputKit offers a variety of formatting options. This helps to format data consistently and improve the input experience.
For instance, you can format a phone number to a specific pattern:
“`swift
import InputKit
let phoneTextField = UITextField()
let phoneFormatter = DefaultTextFormatter(textPattern: “+X (XXX) XXX-XXXX”,
patternSymbol: “X”)
phoneTextField.formatters = [phoneFormatter]
“`
Masking Sensitive Data
InputKit enables you to mask sensitive data, such as credit card numbers, for privacy and security purposes. This allows users to enter data without exposing the full information.
Here’s an example of masking a credit card number:
“`swift
import InputKit
let creditCardTextField = UITextField()
let creditCardMaskPattern = “XXXX-XXXX-XXXX-XXXX”
creditCardTextField.maskFormatter = MaskFormatter(maskPattern: creditCardMaskPattern)
“`
Custom Validation Rules
In addition to the built-in validation rules, InputKit allows you to define custom validation rules to meet specific requirements. Custom rules can be added easily by conforming to the `InputValidator` protocol.
For example, let’s create a custom rule that validates whether the input is an even number:
“`swift
import InputKit
struct EvenNumberValidator: InputValidator {
func validateInputValue(_ inputValue: String) throws {
guard let value = Int(inputValue) else {
throw InputValidationError.invalidFormat
}
guard value % 2 == 0 else {
throw InputValidationError.custom(“Input must be an even number.”)
}
}
}
let numberTextField = UITextField()
numberTextField.validationRules = ValidationRuleSet(rules: [.custom(EvenNumberValidator())])
numberTextField.validateOnInputChange(enabled: true)
“`
Additional Resources
- InputKit GitHub repository: https://github.com/InputKit/InputKit
- InputKit example projects: https://github.com/InputKit/InputKitExamples