Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LoginView.swift
- // UserRegistrationExample
- //
- // Created by Simo Korpikoski on 04/04/17.
- // Copyright © 2017 Simo Korpikoski. All rights reserved.
- //
- import Foundation
- import UIKit
- import FBSDKLoginKit
- import Firebase
- import FirebaseAuth
- let defaults = UserDefaults.standard
- var facebookdata : [String : AnyObject]!
- var firstname = String()
- var lastname = String()
- var fbid = String()
- var currentId = String()
- class LoginViewController: UIViewController, UITextFieldDelegate {
- var dict : [String : AnyObject]!
- var tableData: NSArray = NSArray()
- var userEmailInput = String()
- @IBOutlet weak var usernameTextField: UITextField!
- @IBOutlet weak var userpasswordTextField: UITextField!
- @IBOutlet weak var loginButton: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- //initialize textfields for customization
- self.usernameTextField.delegate = self
- self.userpasswordTextField.delegate = self
- //initialize login button as unabled and check for changes in text fields
- loginButton.isEnabled = false
- usernameTextField.addTarget(self, action: #selector(editingChanged), for: .editingChanged)
- userpasswordTextField.addTarget(self, action: #selector(editingChanged), for: .editingChanged)
- }
- //Login Button functions
- @IBAction func loginButtonTapped(_ sender: AnyObject) {
- if self.usernameTextField.text == "" || self.userpasswordTextField.text == "" {
- //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
- let alertController = UIAlertController(title: "Error", message: "Lisää sähköposti ja salasana.", preferredStyle: .alert)
- let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
- alertController.addAction(defaultAction)
- self.present(alertController, animated: true, completion: nil)
- } else {
- FIRAuth.auth()?.signIn(withEmail: self.usernameTextField.text!, password: self.userpasswordTextField.text!) { (user, error) in
- if error == nil {
- //Print into the console if successfully logged in
- print("You have successfully logged in")
- //Go to the HomeViewController if the login is sucessful
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "profile")
- self.present(vc!, animated: true, completion: nil)
- } else {
- //Tells the user that there is an error and then gets firebase to tell them the error
- let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
- let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
- alertController.addAction(defaultAction)
- self.present(alertController, animated: true, completion: nil)
- }
- }
- }
- }
- //Facebook button functions
- @IBAction func btnFBLoginPressed(_ sender: AnyObject) {
- let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
- fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
- if (error == nil){
- let fbloginresult : FBSDKLoginManagerLoginResult = result!
- if fbloginresult.grantedPermissions != nil {
- if(fbloginresult.grantedPermissions.contains("email"))
- {
- self.getFBUserData()
- fbLoginManager.logOut()
- }
- //segue to next page after a succesfull facebook login
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.4){
- self.performSegue(withIdentifier: "login", sender: self)
- }
- // DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
- // self.performSegue(withIdentifier: "login", sender: self)
- // }
- }
- }
- }
- }
- //Facebook subfunction for getting and printing data to console
- func getFBUserData(){
- if((FBSDKAccessToken.current()) != nil){
- FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
- if (error == nil){
- self.dict = result as! [String : AnyObject]
- //loggedIn Boolean
- defaults.set(true, forKey: "loggedIn")
- //Json parsed into global variables
- facebookdata = result as! [String : AnyObject]
- firstname = facebookdata["first_name"]! as! String
- lastname = facebookdata["last_name"]! as! String
- fbid = facebookdata["id"]! as! String
- print(result!)
- print(self.dict["first_name"]!)
- // print(self.dict["id"]!)
- print(facebookdata["id"]!)
- // print(self.dict["picture"]!)
- }
- })
- }
- }
- //Hide keyboard when user touches outside the keyboard
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- self.view.endEditing(true)
- }
- //Return key press
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- textField.resignFirstResponder()
- return (true)
- }
- //Unable login button if textfields are empty
- func editingChanged(_ textField: UITextField) {
- if textField.text?.characters.count == 1 {
- if textField.text?.characters.first == " " {
- textField.text = ""
- return
- }
- }
- guard
- let username = usernameTextField.text, !username.isEmpty,
- let password = userpasswordTextField.text, !password.isEmpty
- else {
- loginButton.isEnabled = false
- return
- }
- loginButton.isEnabled = true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement