iOS
Swift

[swift4]ログイン機能の実装

英語ですが、こちらの動画のログイン機能を実装してみました。

現在のバージョンだと、色々と違って動かなかったので、動く様にコードを若干変更しています。

ユーザー登録画面

RegisterPageViewController.swift
import UIKit

class RegisterPageViewController: UIViewController {

    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var userPasswordTextField: UITextField!
    @IBOutlet weak var repeatPasswordTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func registerButtonTapped(_ sender: Any) {

        let userName = userNameTextField.text
        let userPassword = userPasswordTextField.text
        let userRepeatPassword = repeatPasswordTextField.text

        // 空白確認
        if(userName == "" || userPassword == "" || userRepeatPassword == ""){
            //アラートメッセージ
            displayMyAlertMessage(userMessage: "全てのフォームに入力してください。")
            return
        }

        //パスワード一致確認
        if(userPassword != userRepeatPassword)
        {
            displayMyAlertMessage(userMessage: "パスワードが一致していません。")
            return
        }

        // データ登録
        UserDefaults.standard.set(userName, forKey:"userName")
        UserDefaults.standard.set(userPassword, forKey:"userPassword")
        //UserDefaults.standard.synchronize();

        // メッセージアラートなど
        let myAlert = UIAlertController(title:"Alert", message: "どうも、登録完了!!", preferredStyle:  UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.default){
            action in self.dismiss(animated: true, completion:nil)
        }
        myAlert.addAction(okAction)
        self.present(myAlert, animated:true,completion:nil)

    }

    func displayMyAlertMessage(userMessage: String){

        let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle:  UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.default, handler:nil)
        myAlert.addAction(okAction);
        self.present(myAlert,animated:true, completion:nil)

    }

}

ログイン画面

LoginViewController.swift
import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var userPasswordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func loginButtonTapped(_ sender: Any) {

        let userName = userNameTextField.text;
        let userPassword = userPasswordTextField.text;
        let userNameStored = UserDefaults.standard.string(forKey: "userName")
        let userPasswordStored = UserDefaults.standard.string(forKey: "userPassword")
        if(userNameStored == userName){

            if(userPasswordStored == userPassword){

                // ログイン!
                UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
                //UserDefaults.standard.synchronize()
                self.dismiss(animated: true, completion:nil)

            }

        }

    }

}

メイン画面

ViewController.swift
import UIKit

class ViewController: UIViewController {
    //@IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(_ animated: Bool) {

        let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")

        if(!isUserLoggedIn)
        {
            self.performSegue (withIdentifier: "loginView", sender: self)
        }

    }

    @IBAction func LogoutButtonTapped(_ sender: Any) {

        UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
        //UserDefaults.standard.synchronize()
        self.performSegue(withIdentifier: "loginView", sender: self)

    }

}

ご参考までに。

[2018年3月22日更新]
UserDefaults.standard.synchronize()は、全てコメントアウトしました。