MSLogin-Form 0.1.3.7

MSLogin-Form 0.1.3.7

Maintained by Claudio Madureira Silva Filho.



MSLogin-Form 0.1.3.7

  • By
  • Claudio Madureira Silva Filho

MSLogin-Form

Platform Swift License

Description

MSLogin-Form was developed by a student of Control and Automation Engineer that have been working with iOS applications for 1,5 year. Feeling the necessity to have a form in all applications that he worked, this library was build for those who need a form into their application. It also has a controller class for login that handle inputs from user and enables or not the login button based on an email address and the password length. It's easy to setup and you can find it on the example.

This library is multi-language. It contains English and Portuguese versions. See how to use Portuguese version bellow.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

  1. RegisterViewController
import UIKit

import MSLogin_Form

class RegisterViewController: UIViewController {

  @IBOutlet weak var btnBack: UIButton!
  @IBOutlet weak var btnRegister: UIButton!
  @IBOutlet weak var txfName: UITextField!
  @IBOutlet weak var txfPhone: UITextField!
  @IBOutlet weak var txfEmail: UITextField!
  @IBOutlet weak var txfPassword: UITextField!
  @IBOutlet weak var txfPasswordConfirm: UITextField!

  var form: MSForm!

  override func viewDidLoad() {
    super.viewDidLoad()
    self.setupForm()
  }

  func setupForm() {
    let fieldName = MSField(textField: txfName,
                            type: .normal,
                            placeholder: "Name")
    let fieldPhone = MSField(textField: txfPhone,
                            type: .number,
                            placeholder: "Number",
                            mask: "(##) # ####-####",
                            changeAutomatically: true)
    let fieldEmail = MSField(textField: txfEmail,
                            type: .email,
                            placeholder: "Email")
    let fieldPass = MSField(textField: txfPassword,
                            type: .password,
                            placeholder: "Password")
    let fieldPassConfir = MSField(textField: txfPasswordConfirm,
                            type: .passwordConfirm,
                            placeholder: "Password Confirm")
    self.form = MSForm(fields: [fieldName, fieldPhone, fieldEmail, fieldPass, fieldPassConfir],
                        passwordLength: 6,
                        buttonGo: self.btnRegister,
                        buttonGoAction: { inputs in
                                    print("INPUTS:")
                                    for input in inputs {
                                      print(">> ", input)
                                    }
                                    self.dismissSelf() })
    self.form.delegate = self
    self.btnBack.addTarget(self, action: #selector(self.dismissSelf), for: .touchUpInside)
  }

  @objc func dismissSelf() {
    self.dismiss(animated: true, completion: nil)
  }

}

extension RegisterViewController: MSFormDelegate {

  func fieldDidChange(_ field: MSField) {	}

  func fieldShouldReturn(_ field: MSField) -> Bool { return true }

  func fieldErrorHandler(_ field: MSField, message: String) {
    self.showAlertCommon(title: "Error!", message: message, handler: nil)
  }

}


extension UIViewController {

  func showAlertCommon(title: String, message: String?, handler: ((UIAlertAction) -> Void)?) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let btnOk = UIAlertAction(title: "Ok", style: .default, handler: handler)
    alert.addAction(btnOk)
    self.present(alert, animated: true, completion: nil)
  }

}
  1. RegisterTableViewController
import UIKit

import MSLogin_Form

class RegisterTableViewController: UITableViewController {

  var fields: [MSField] = []
  var field0: MSField?
  var field1: MSField?
  var field2: MSField?
  var field3: MSField?
  var field4: MSField?
  var field5: MSField?

  override func viewDidLoad() {
    super.viewDidLoad()
    self.setupTableView()
    self.setupButtonDone()
  }

  func setupButtonDone() {
    let rightButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.handleFieldsInput))
    self.navigationItem.rightBarButtonItem = rightButton
  }

  func setupTableView() {
    self.tableView.allowsSelection = false
    self.tableView.separatorColor = .clear
    let identifiers: [String] = ["DoubleFieldTableViewCell"]
    for identifier in identifiers {
      let nib = UINib(nibName: identifier, bundle: nil)
      self.tableView.register(nib, forCellReuseIdentifier: identifier)
    }
    self.tableView.reloadData()
  }

  func addFieldIfNotInForm(field: MSField) {
    if !self.fields.contains(field) {
      self.fields.append(field)
    }
  }

  override func dismissSelf() {
    for field in self.fields  {
      field.textField?.resignFirstResponder()
    }
    self.dismiss(animated: true, completion: nil)
  }

  @objc func handleFieldsInput() {
    guard self.fields.count == 6 else {
      self.showAlertCommon(title: "Error!", message: "You didn't check all fields!", handler: nil)
      return
    }
    MSForm.verifyFields(fields: self.fields, passwordLength: 6, action: { inputs in
        print("INPUTS:")
        for input in inputs {
        print(">> ", input)
        }
        self.dismissSelf()
      }, error: { field, errorMessage in
        self.showAlertCommon(title: "Error!", message: errorMessage, handler: { _ in
          DispatchQueue.main.async {
            let _ = field.textField?.becomeFirstResponder()
          }
        })
    })
  }

  // MARK: - Table view data source

  override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DoubleFieldTableViewCell", for: indexPath) as! DoubleFieldTableViewCell
    switch indexPath.row {
    case 0:
      cell.setInfo(setupFields: { textFieldLeft, textFieldRight in
        if self.field0 == nil {
          let field = MSField(textField: textFieldLeft, type: .normal, placeholder: "Name")
          self.field0 = field
          self.addFieldIfNotInForm(field: field)
          self.field0?.delegate = self
        }
        if self.field1 == nil {
          let field = MSField(textField: textFieldRight, type: .email, placeholder: "Email")
          self.field1 = field
          self.addFieldIfNotInForm(field: field)
          self.field1?.delegate = self
        }
      })
    case 1:
      cell.setInfo(setupFields: { textFieldLeft, textFieldRight in
        if self.field2 == nil {
          let field = MSField(textField: textFieldLeft, type: .pickerString, placeholder: "Sex", values: ["Male", "Female", "I'd rather not say"])
          self.field2 = field
          self.addFieldIfNotInForm(field: field)
          self.field2?.delegate = self
        }
        if self.field3 == nil {
          let field = MSField(textField: textFieldRight, type: .number, placeholder: "Phone Number", mask: "(##) # ####-####", changeAutomatically: true)
          self.field3 = field
          self.addFieldIfNotInForm(field: field)
          self.field3?.delegate = self
        }
      })
    default:
      cell.setInfo(setupFields: { textFieldLeft, textFieldRight in
        if self.field4 == nil {
          let field = MSField(textField: textFieldLeft, type: .password, placeholder: "Password")
          self.field4 = field
          self.addFieldIfNotInForm(field: field)
          self.field4?.delegate = self
        }
        if self.field5 == nil {
          let field = MSField(textField: textFieldRight, type: .passwordConfirm, placeholder: "Password Confirm")
          self.field5 = field
          self.addFieldIfNotInForm(field: field)
          self.field5?.delegate = self
        }
      })
    }
    return cell
  }

}

extension RegisterTableViewController: MSFieldDelegate {

  func fieldDidChange(_ field: MSField) {
    self.addFieldIfNotInForm(field: field)
  }

  func fieldShouldReturn(_ field: MSField) -> Bool {
    switch field {
    case field0:
      self.field1?.textField?.becomeFirstResponder()
    case field1:
      self.field2?.textField?.becomeFirstResponder()
    case field2:
      self.field3?.textField?.becomeFirstResponder()
    case field3:
      self.field4?.textField?.becomeFirstResponder()
    case field4:
      self.field5?.textField?.becomeFirstResponder()
    case field5:
      self.handleFieldsInput()
    default:
      break
    }
    return true
  }

}
  1. Login Controller
import UIKit

import MSLogin_Form

class LoginViewController: MSLoginVC {

  @IBOutlet weak var txfEmail: UITextField!
  @IBOutlet weak var txfPassword: UITextField!
  @IBOutlet weak var btnLogin: UIButton!
  @IBOutlet weak var btnPassRecover: UIButton!
  @IBOutlet weak var btnRegister: UIButton!

  override func viewDidLoad() {
    super.viewDidLoad()
    self.configureVC()
  }

  func configureVC() {
    self.textFieldEmail         = txfEmail
    self.textFieldPassword      = txfPassword
    self.buttonLogin            = btnLogin
    self.buttonRegister         = btnRegister
    self.buttonPasswordRecover  = btnPassRecover
    self.passwordLength         = 10

    self.loginAction = {
      // Type here you comunication function and all that should happen when the user press the log in button
      print("LogInTapped")
    }

    self.recoverPasswordAction = {
      // Type here you comunication function and all that should happen when the user press the recover password button
      print("RecoverPasswordTapped")
    }

    self.buttonRegisterAction = {
      // Type here you comunication function and all that should happen when the user press the Go button
      print("RegisterTapped")
      // ViewController
      // let vc = self.storyboard?.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
      // self.present(vc, animated: true, completion: nil)

      // TableViewController
      let vc = RegisterTableViewController()
      vc.title = "RegisterTableViewController"
      let nav = UINavigationController(rootViewController: vc)
      vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: vc, action: #selector(vc.dismissSelf))
      self.present(nav, animated: true, completion: nil)
    }
  }

  override func textFieldDidChange(textField: UITextField) {
    // In case that you need to do smething else
    print("textChanged")
  }

}

Multi-Language

If you want to use multi-language or just portuguese version, you have to add Portuguese language in your project Info tab. See where bellow:

github-small

Requirements

This library requires iOS 8.0+ and Swift 4 with XCode 9.

Installation

MSLogin-Form is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MSLogin-Form'

Author

Claudio Madureira Silva Filho, [email protected]

License

MSLogin-Form is available under the MIT license. See the LICENSE file for more info.