PSMeter 0.1.5

PSMeter 0.1.5

Maintained by Omar, Ahmed Tarek, Ahmed Tarek.



 
Depends on:
Navajo-Swift>= 0
GTProgressBar>= 0
 

PSMeter 0.1.5

  • By
  • Baianat

PSMeter

A password strength meter for iOS.

Version License Platform

Quick Start

Customization

Delegate (Callback)

Password Strength Algorithm

Installation

Quick Start:

1- Drag View object from the Object Library into your storyboard.

2- Set the view's class to PSMeter in the Identity Inspector. Set its module property to PSMeter.

3- Add import PSMeter to the header of your view controller.

4- Create an outlet in the corresponding view controller.

@IBOutlet weak var psMeter: PSMeter!

5- Invoke updateStrengthIndication(password:String) on PSMeter instance to update the password strength indicator.

Note

updateStrengthIndication(password:String) should be called whenever the password text is changed to update the indicator appopriately.

This can easily be achieved in a couple of ways..

  • Using RxSwift ...
passwordTextField.rx.text.changed.subscribe(onNext: { (password) in
    self.psMeter.updateStrengthIndication(password: password ?? "")
}).disposed(by: bag)
  • Using IBActions ...

1- create an @IBAction in your view controller for Editing Changed.

2- Invoke updateStrengthIndication in that action.

@IBAction func passwordDidChange(_ sender: Any) {
    let password = passwordTextField.text ?? ""
    psMeter.updateStrengthIndication(password: password)
}

Customization:

View Customization:

You can customize PSMeter for these attributes:

Attribute Definition Default value
titleText the title across the strength value "Password Strength"
titleTextColor the text color for the title .black
font the font for both the title and the strength labels .systemFont(ofSize: 14)
barBackgroundColor the background color of the strength bar .clear

Example:

override func viewDidLoad() {
    ...
    psMeter.titleText = "Password Robustness"
    psMeter.tintColor = .red
    psMeter.font = UIFont.boldSystemFont(ofSize: 14)
}

States Customization:

You can also customize text, text color, progress color for each of the 6 strength states.

These attributes are defined using StateDecorator object as follows...

let weakPasswordDecorator = StateDecorator(text: "Weak", textColor: .red, progressColor: .red)

To customize all the stated you only have to set the six decorators for the six different states (empty, very weak, weak, fair, strong, very strong).

This can easily be achieved using PStrengthViewStatesDecorator object, the object combining all the decorators.

Example: The default PStrengthViewStatesDecorator is implemented as follows...

let defaultStatesDecorator =  PStrengthViewStatesDecorator(
    emptyPasswordDecorator:StateDecorator(text: "--", textColor: .gray, progressColor: .gray),
    veryWeakPasswordDecorator: StateDecorator(text: "Very Weak", textColor: .red, progressColor: .red),
    weakPasswordDecorator: StateDecorator(text: "Weak", textColor: .orange, progressColor: .orange),
    fairPasswordDecorator: StateDecorator(text: "Fair", textColor: .yellow, progressColor: .yellow),
    strongPasswordDecorator: StateDecorator(text: "Strong", textColor: .green, progressColor: .green),
    veryStrongPasswordDecorator: StateDecorator(text: "Very Strong", textColor: .blue, progressColor: .blue)
)
psMeter.statesDecorator = defaultStatesDecorator

Delegate (Callback):

You can observe for whenever the password strength changes, just set the the delegate on PSMeter and implement PsMeterDelegate as follows...

...
override func viewDidLoad() {
    super.viewDidLoad()
        ...
        psMeter.delegate = self
    }
}

extension ViewController : PSMeterDelegate {
    func psMeter(_ psMeter: PSMeter, didChangeStrength passwordStrength: PasswordStrength) {

    }
}

Alternatively, you can get the current password strength using passwordStrength on PSMeter as follows...

let currentStrength = psMeter.passwordStrength

Either way passwordStrength is of type PasswordStrength enum that can have on of the following values...

let currentStrength = psMeter.passwordStrength
switch currentStrength {
    case .empty:
        print("empty")
    case .veryWeak:
        print("veryWeak")
    case .weak:
        print("weak")
    case .fair:
        print("fair")
    case .strong:
        print("strong")
    case .veryStrong:
        print("veryStrong")
    default:
        break
}

Password Strength Algorithm:

Default Algorithm:

The default algorithm for determining password strength is based on Navajo-Swift by @jasonnam.

Custom Algorithm:

You can easily altar the algorithm defining how password strength is estimated, just set passwordEstimator on PSMeter and implement PasswordEstimator as follows...

Example: A custom algorithm that judges a password as weak only if its length is less than 8 character othewise fair can be defined as follows...

...
override func viewDidLoad() {
        ...
        psMeter.passwordEstimator = self
    }
}

extension ViewController : PasswordEstimator {
    func estimatePassword(_ password: String) -> PasswordStrength {
        if password.count = 8 {
            return .weak
        }else {
            return .fair
        }
    }
}

Installation:

To install PSMeter , simply add the following line to your Podfile:

pod 'PSMeter'

Dependency:

PSMeter uses Navajo-Swift and GTProgressBar under its hood, they gets automatically installed with your pods.

License:

PSMeter is available under the MIT license. See the LICENSE file for more info.