Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class LoginViewController: UIViewController {
- let database = Database.sharedInstance
- @IBOutlet weak var studyID: UITextField!
- @IBOutlet weak var userID: UITextField!
- @IBOutlet weak var signInButton: UIButton!
- @IBAction func signInPressed(sender: AnyObject) {
- if (!studyID.text!.isEmpty && !userID.text!.isEmpty){
- self.database.validate(userID.text!, study:studyID.text!, pass:userID.text!) {
- (value: Bool) in
- if value {
- self.performSegueWithIdentifier("loggedIn", sender: nil)
- } else {
- let errorController = UIAlertController(
- title: "Error",
- message: "There was an error with authentication.",
- preferredStyle: .Alert)
- // set an action button on the alert
- let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
- (errorController: UIAlertAction!) -> Void in
- })
- // add the action
- errorController.addAction(okAction)
- // show the alert to the user with animation
- self.presentViewController(errorController, animated: true, completion: nil)
- }
- }
- } else {
- let blankController = UIAlertController(
- title: "Error",
- message: "Please fill in all fields.",
- preferredStyle: .Alert)
- // set an action button on the alert
- let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
- (blankController: UIAlertAction!) -> Void in
- })
- // add the action
- blankController.addAction(okAction)
- // show the alert to the user with animation
- self.presentViewController(blankController, animated: true, completion: nil)
- }
- }
- }
- import Foundation
- import Firebase
- class Database {
- var firebaseRef = Firebase(url:"https://<UNIQUE>.firebaseio.com")
- class var sharedInstance: Database {
- struct Data {
- static var instance: Database?
- static var token: dispatch_once_t = 0
- }
- dispatch_once(&Data.token) {
- Data.instance = Database()
- }
- return Data.instance!
- }
- var uid : String!
- var Question1 : String!
- var Question2 : String!
- var Question3 : String!
- var Question4 : String!
- var Score : Int!
- var Question1 : String!
- var Question2 : String!
- var Question3 : String!
- var Question4 : String!
- var Score : Int!
- var validated = false
- func validate(user: String, study: String, pass: String, completionHandler:(result: Bool) -> Void) {
- firebaseRef.authUser(user+"@email.com", password: pass,
- withCompletionBlock: { error, authData in
- if error != nil {
- completionHandler(result: false)
- return
- } else {
- self.uid = authData.uid
- NSLog(authData.uid)
- }
- })
- let usersRef = Firebase(url: "https://<UNIQUE>.firebaseio.com/users")
- usersRef.observeEventType(FEventType.Value, withBlock: { (snapshot) in
- if let value = snapshot.value.objectForKey("study") as? String {
- if value == study {
- completionHandler(result: true)
- return
- } else {
- completionHandler(result: false)
- return
- }
- } else {
- completionHandler(result: false)
- return
- }
- })
- }
- func sendQuestionData(type: String) {
- let ref = Firebase(url: "https://<UNIQUE>.firebaseio.com/users/" + uid + "/weeks/" + "1" + "/" + type + "/")
- var json = NSDictionary()
- if type == "Condition1" {
- json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
- "Question4" : Question4, "Score" : Score]
- } else {
- json = ["Question1" : Question1, "Question2" : Question2, "Question3" : Question3,
- "Question4" : Question4, "Score" : Score]
- }
- NSLog(String(json))
- ref.setValue(json)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement