Created
April 14, 2016 07:52
-
-
Save charleshkang/a678084cf2823e5707de1bd68c0ee6b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CMLoginViewController.swift | |
// Coffee Mapper | |
// | |
// Created by Charles Kang on 4/13/16. | |
// Copyright © 2016 Charles Kang. All rights reserved. | |
// | |
import UIKit | |
class CMLoginViewController: UIViewController | |
{ | |
@IBOutlet weak var signUpButtonTapped: UIButton! | |
@IBOutlet weak var signInButtonTapped: UIButton! | |
@IBOutlet weak var passwordTextField: UITextField! | |
@IBOutlet weak var emailTextField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.hideKeyboardWhenTappedAround() | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
if NSUserDefaults.standardUserDefaults().valueForKey("uid") != nil && DataService.dataService.CURRENT_USER_REF.authData != nil { | |
self.performSegueWithIdentifier("CurrentlyLoggedIn", sender: nil) | |
} | |
} | |
@IBAction func signInButtonTapped(sender: AnyObject) | |
{ | |
let email = emailTextField.text | |
let password = passwordTextField.text | |
if email != "" && password != "" { | |
DataService.dataService.BASE_REF.authUser(email, password: password, withCompletionBlock: { error, authData in | |
if error != nil { | |
print(error) | |
self.loginErrorAlert("Oops!", message: "Check your username and password.") | |
} else { | |
NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: "uid") | |
self.performSegueWithIdentifier("CurrentlyLoggedIn", sender: nil) | |
} | |
}) | |
} else { | |
loginErrorAlert("Oops!", message: "Don't forget to enter your email and password.") | |
} | |
} | |
func loginErrorAlert(title: String, message: String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) | |
let action = UIAlertAction(title: "Ok", style: .Default, handler: nil) | |
alert.addAction(action) | |
presentViewController(alert, animated: true, completion: nil) | |
} | |
} | |
extension UIViewController { | |
func hideKeyboardWhenTappedAround() { | |
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) | |
view.addGestureRecognizer(tap) | |
} | |
func dismissKeyboard() { | |
view.endEditing(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment