create a login page in swift

Here is a sample code to create a login page in Swift using Xcode:

Step 1: Create a new Xcode project and select "Single View App" under "App" category.

Step 2: Add two text fields on the view controller for email and password.

Step 3: Add a button on the view controller for login.

Step 4: Create an action for the login button.

main.swift
@IBAction func loginButtonTapped(_ sender: Any) {
    let email = emailTextField.text
    let password = passwordTextField.text
    
    if email == "" || password == "" {
        let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
    
        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(defaultAction)
    
        present(alertController, animated: true, completion: nil)
    }
    else {
        // Your code to log in the user goes here
    }
}
594 chars
17 lines

Step 5: Add outlets for the text fields.

main.swift
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
99 chars
3 lines

That's it! You now have a basic login page in Swift.

related categories

gistlibby LogSnag