Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. import UIKit
  2.  
  3. class LoginViewController: UIViewController {
  4.  
  5. let database = Database.sharedInstance
  6.  
  7. @IBOutlet weak var studyID: UITextField!
  8. @IBOutlet weak var userID: UITextField!
  9. @IBOutlet weak var signInButton: UIButton!
  10.  
  11.  
  12. @IBAction func signInPressed(sender: AnyObject) {
  13.  
  14. if (!studyID.text!.isEmpty && !userID.text!.isEmpty){
  15. self.database.validate(userID.text!, study:studyID.text!, pass:userID.text!) {
  16. (value: Bool) in
  17. if value {
  18. self.performSegueWithIdentifier("loggedIn", sender: nil)
  19. } else {
  20. let errorController = UIAlertController(
  21. title: "Error",
  22. message: "There was an error with authentication.",
  23. preferredStyle: .Alert)
  24. // set an action button on the alert
  25. let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
  26. (errorController: UIAlertAction!) -> Void in
  27. })
  28. // add the action
  29. errorController.addAction(okAction)
  30. // show the alert to the user with animation
  31.  
  32. self.presentViewController(errorController, animated: true, completion: nil)
  33.  
  34. }
  35. }
  36. } else {
  37. let blankController = UIAlertController(
  38. title: "Error",
  39. message: "Please fill in all fields.",
  40. preferredStyle: .Alert)
  41. // set an action button on the alert
  42. let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
  43. (blankController: UIAlertAction!) -> Void in
  44. })
  45. // add the action
  46. blankController.addAction(okAction)
  47. // show the alert to the user with animation
  48.  
  49. self.presentViewController(blankController, animated: true, completion: nil)
  50. }
  51. }
  52.  
  53.  
  54. }
  55.  
  56. import Foundation
  57. import Firebase
  58.  
  59. class Database {
  60.  
  61. var firebaseRef = Firebase(url:"https://<UNIQUE>.firebaseio.com")
  62.  
  63. class var sharedInstance: Database {
  64. struct Data {
  65. static var instance: Database?
  66. static var token: dispatch_once_t = 0
  67. }
  68.  
  69. dispatch_once(&Data.token) {
  70. Data.instance = Database()
  71. }
  72.  
  73. return Data.instance!
  74. }
  75.  
  76. var uid : String!
  77.  
  78. var Question1 : String!
  79. var Question2 : String!
  80. var Question3 : String!
  81. var Question4 : String!
  82. var Score : Int!
  83.  
  84. var Question1 : String!
  85. var Question2 : String!
  86. var Question3 : String!
  87. var Question4 : String!
  88. var Score : Int!
  89.  
  90.  
  91. var validated = false
  92.  
  93. func validate(user: String, study: String, pass: String, completionHandler:(result: Bool) -> Void) {
  94.  
  95. firebaseRef.authUser(user+"@email.com", password: pass,
  96. withCompletionBlock: { error, authData in
  97. if error != nil {
  98. completionHandler(result: false)
  99. return
  100. } else {
  101. self.uid = authData.uid
  102. NSLog(authData.uid)
  103. }
  104. })
  105.  
  106. let usersRef = Firebase(url: "https://<UNIQUE>.firebaseio.com/users")
  107. usersRef.observeEventType(FEventType.Value, withBlock: { (snapshot) in
  108. if let value = snapshot.value.objectForKey("study") as? String {
  109. if value == study {
  110. completionHandler(result: true)
  111. return
  112. } else {
  113. completionHandler(result: false)
  114. return
  115. }
  116. } else {
  117. completionHandler(result: false)
  118. return
  119. }
  120. })
  121. }
  122.  
  123. func sendQuestionData(type: String) {
  124. let ref = Firebase(url: "https://<UNIQUE>.firebaseio.com/users/" + uid + "/weeks/" + "1" + "/" + type + "/")
  125. var json = NSDictionary()
  126. if type == "Condition1" {
  127. json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
  128. "Question4" : Question4, "Score" : Score]
  129. } else {
  130. json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
  131. "Question4" : Question4, "Score" : Score]
  132. }
  133. NSLog(String(json))
  134. ref.setValue(json)
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement