Har lekt lite med Swift och Xcode under semestern. Är alltså en total novis när det kommer till programmering. Bygger en enkel app och lär mig eftersom. Jag har lyckats med att användare kan registrera sig och logga in, men jag lyckas inte skicka användaren vidare till min main ViewController.

LogInViewController.swift:

Citat:

import UIKit
import Firebase

class LogInViewController: UIViewController {

@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

@IBAction func loginAction(sender: AnyObject)
{
if self.emailField.text == "" || self.passwordField.text == ""
{
let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil
{
self.emailField.text = ""
self.passwordField.text = ""
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}

}
}
}
}

SignUpViewController.swift:

Citat:

import UIKit
import Firebase

class SignUpViewController: UIViewController {

@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

@IBAction func createAccountAction(sender: AnyObject)
{
if self.emailField.text == "" || self.passwordField.text == ""
{
let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil
{
self.emailField.text = ""
self.passwordField.text = ""
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}

}
}
}
// Returns the user to LogInViewController
@IBAction func Cancel(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

Tanken är som sagt att skicka användaren till min main vilket är en UITabBarController. Misstänker att jag behöver en segue till denna controller, men bör jag använda en med push, eller en modular? Kopplar jag min loginAction till UITabBarController med en segue typen push så passeras reglerna som jag har satt upp, använder jag modular så får jag en krasch.

Edit: har läst på lite gällande performSegueWithIdentifier, men får ändå inte till det, men misstänker att det är sättet att lösa det på.

Senast redigerat 2016-09-04 13:46