Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Last active May 8, 2019 09:23
Show Gist options
  • Save KalpeshTalkar/1f8078f0e9340125f24102fdd4c73a82 to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/1f8078f0e9340125f24102fdd4c73a82 to your computer and use it in GitHub Desktop.
A customised UIAlertController for reset password functionality written in Swift.
//
// ResetPasswordAlert.swift
//
// Created by Kalpesh on 06/05/19.
// Copyright © 2019 Kalpesh Talkar. All rights reserved.
//
// For support: https://gist.github.com/KalpeshTalkar/1f8078f0e9340125f24102fdd4c73a82
import UIKit
/// Default title of the alert view.
fileprivate let TITLE = "Forgot your password?"
/// Default message of the alert view.
fileprivate let MESSAGE = "Enter your registererd e-mail address and we'll send a link to reset your password."
/// ResetPasswordAlert is a UIAlertController sub class customised to show reset password alert with a text field to enter email with submit and cancel buttons.The class requires Strin extension and UITextField extension to validate the email address.
public class ResetPasswordAlert: UIAlertController {
/// Handler called when submit button is clicked.
public typealias ResetPasswordHandler = ((String) -> Void)
/// Submit action button.
private var submitAction = UIAlertAction()
/// Private init method.
///
/// - Parameters:
/// - nibNameOrNil: Xib name. Optional parameter.
/// - nibBundleOrNil: Bundle of the xib. Optional parameter.
private override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
/// Required init method.
///
/// - Parameter aDecoder: NSCoder.
internal required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// Init ResetPasswordAlert.
///
/// - Parameters:
/// - title: Title for the alert.
/// - message: Message for the alert.
/// - handler: Handler called when submit button is clicked with entered email as parameter.
//public convenience init(title: String = TITLE, message: String = MESSAGE, handler: ResetPasswordHandler? = nil) {
public convenience init(title: String? = nil, message: String? = nil, handler: ResetPasswordHandler? = nil) {
let ttl = title ?? TITLE
let msg = message ?? MESSAGE
self.init(title: ttl, message: msg, preferredStyle: .alert)
submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
if handler != nil {
if let textField = self.textFields?[0] {
handler?(textField.text ?? "")
}
}
}
submitAction.isEnabled = false
addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
addAction(submitAction)
addTextField { (textField) in
textField.textContentType = .emailAddress
textField.addTarget(self, action: #selector(self.textFieldTextChanged(textField:)), for: .editingChanged)
}
}
/// Method to handle text change.
/// The methods enables the submit button if the entered email is valid.
///
/// - Parameter textField: UITextField reference.
@objc internal func textFieldTextChanged(textField: UITextField) {
submitAction.isEnabled = textField.hasValidEmail()
}
/// Show ResetPasswordAlert.
///
/// - Parameters:
/// - vc: UIViewController reference in which the alert is to be presented.
/// - completion: Completion block called after presenting the alert.
public func present(in vc: UIViewController, completion: (() -> Void)? = nil) {
vc.present(self, animated: true, completion: completion)
}
}
@KalpeshTalkar
Copy link
Author

KalpeshTalkar commented May 8, 2019

Usage:

let pwdResetAlert = ResetPasswordAlert() { (email) in
    print("Email: \(email)")
}
present(pwdResetAlert, animated: true, completion: nil)

OR

ResetPasswordAlert(handler: { (email) in
    print("Email: \(email)")
}).present(in: self)

You can also set your custom title and message like below:

let pwdResetAlert = ResetPasswordAlert(title: "My Title", message: "My Message") { (email) in
    print("Email: \(email)")
}
present(pwdResetAlert, animated: true, completion: nil)

OR

ResetPasswordAlert(title: "My Title", message: "My Message", handler: { (email) in
    print("Email: \(email)")
}).present(in: self)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment