Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. //
  2. // LoginView.swift
  3. // UserRegistrationExample
  4. //
  5. // Created by Simo Korpikoski on 04/04/17.
  6. // Copyright © 2017 Simo Korpikoski. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11. import FBSDKLoginKit
  12. import Firebase
  13. import FirebaseAuth
  14.  
  15. let defaults = UserDefaults.standard
  16.  
  17. var facebookdata : [String : AnyObject]!
  18. var firstname = String()
  19. var lastname = String()
  20. var fbid = String()
  21. var currentId = String()
  22.  
  23. class LoginViewController: UIViewController, UITextFieldDelegate {
  24.  
  25. var dict : [String : AnyObject]!
  26. var tableData: NSArray = NSArray()
  27. var userEmailInput = String()
  28.  
  29. @IBOutlet weak var usernameTextField: UITextField!
  30. @IBOutlet weak var userpasswordTextField: UITextField!
  31. @IBOutlet weak var loginButton: UIButton!
  32.  
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35.  
  36. //initialize textfields for customization
  37. self.usernameTextField.delegate = self
  38. self.userpasswordTextField.delegate = self
  39.  
  40. //initialize login button as unabled and check for changes in text fields
  41. loginButton.isEnabled = false
  42. usernameTextField.addTarget(self, action: #selector(editingChanged), for: .editingChanged)
  43. userpasswordTextField.addTarget(self, action: #selector(editingChanged), for: .editingChanged)
  44.  
  45.  
  46. }
  47.  
  48. //Login Button functions
  49. @IBAction func loginButtonTapped(_ sender: AnyObject) {
  50.  
  51. if self.usernameTextField.text == "" || self.userpasswordTextField.text == "" {
  52.  
  53. //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
  54.  
  55. let alertController = UIAlertController(title: "Error", message: "Lisää sähköposti ja salasana.", preferredStyle: .alert)
  56.  
  57. let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
  58. alertController.addAction(defaultAction)
  59.  
  60. self.present(alertController, animated: true, completion: nil)
  61.  
  62. } else {
  63.  
  64. FIRAuth.auth()?.signIn(withEmail: self.usernameTextField.text!, password: self.userpasswordTextField.text!) { (user, error) in
  65.  
  66. if error == nil {
  67.  
  68. //Print into the console if successfully logged in
  69. print("You have successfully logged in")
  70.  
  71. //Go to the HomeViewController if the login is sucessful
  72.  
  73. // let vc = self.storyboard?.instantiateViewController(withIdentifier: "profile")
  74. // self.present(vc!, animated: true, completion: nil)
  75.  
  76. DispatchQueue.main.async {
  77. self.performSegue(withIdentifier: "login", sender: self)
  78. }
  79. } else {
  80.  
  81. //Tells the user that there is an error and then gets firebase to tell them the error
  82. let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
  83.  
  84. let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
  85. alertController.addAction(defaultAction)
  86.  
  87. self.present(alertController, animated: true, completion: nil)
  88. }
  89. }
  90. }
  91.  
  92. }
  93.  
  94. //Facebook button functions
  95. @IBAction func btnFBLoginPressed(_ sender: AnyObject) {
  96. let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
  97. fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
  98. if (error == nil){
  99. let fbloginresult : FBSDKLoginManagerLoginResult = result!
  100. if fbloginresult.grantedPermissions != nil {
  101. if(fbloginresult.grantedPermissions.contains("email"))
  102. {
  103.  
  104. self.getFBUserData()
  105. fbLoginManager.logOut()
  106. }
  107.  
  108.  
  109. //segue to next page after a succesfull facebook login
  110. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4){
  111. self.performSegue(withIdentifier: "login", sender: self)
  112.  
  113. }
  114.  
  115. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  116. // self.performSegue(withIdentifier: "login", sender: self)
  117. // }
  118.  
  119.  
  120. }
  121. }
  122. }
  123. }
  124.  
  125.  
  126.  
  127. //Facebook subfunction for getting and printing data to console
  128. func getFBUserData(){
  129. if((FBSDKAccessToken.current()) != nil){
  130. FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
  131. if (error == nil){
  132. self.dict = result as! [String : AnyObject]
  133.  
  134. //loggedIn Boolean
  135. defaults.set(true, forKey: "loggedIn")
  136.  
  137. //Json parsed into global variables
  138. facebookdata = result as! [String : AnyObject]
  139. firstname = facebookdata["first_name"]! as! String
  140. lastname = facebookdata["last_name"]! as! String
  141. fbid = facebookdata["id"]! as! String
  142.  
  143. print(result!)
  144. print(self.dict["first_name"]!)
  145. // print(self.dict["id"]!)
  146. print(facebookdata["id"]!)
  147. // print(self.dict["picture"]!)
  148. }
  149. })
  150. }
  151. }
  152.  
  153.  
  154.  
  155.  
  156. //Hide keyboard when user touches outside the keyboard
  157. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  158. self.view.endEditing(true)
  159. }
  160.  
  161. //Return key press
  162. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  163. textField.resignFirstResponder()
  164. return (true)
  165. }
  166.  
  167.  
  168. //Unable login button if textfields are empty
  169. func editingChanged(_ textField: UITextField) {
  170. if textField.text?.characters.count == 1 {
  171. if textField.text?.characters.first == " " {
  172. textField.text = ""
  173. return
  174. }
  175. }
  176. guard
  177. let username = usernameTextField.text, !username.isEmpty,
  178. let password = userpasswordTextField.text, !password.isEmpty
  179. else {
  180. loginButton.isEnabled = false
  181. return
  182. }
  183. loginButton.isEnabled = true
  184. }
  185.  
  186.  
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement