No Description

ResearchContainerViewController.swift 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // ResearchContainerViewController.swift
  3. // EncuestaMarle
  4. //
  5. // Created by Tatiana Castro on 5/20/19.
  6. // Copyright © 2019 Marle. All rights reserved.
  7. //
  8. import UIKit
  9. import ResearchKit
  10. var response_token = false
  11. //func checkToken(){
  12. //
  13. // let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/checkToken.php")! as URL)
  14. // request.httpMethod = "POST"
  15. // let postString = "data=\(token)"
  16. // request.httpBody = postString.data(using: String.Encoding.utf8)
  17. //
  18. // let task = URLSession.shared.dataTask(with: request as URLRequest) {
  19. // data, response, error in
  20. //
  21. // if error != nil {
  22. // print("error=\(String(describing: error))")
  23. // return
  24. // }
  25. //
  26. // print("response = \(String(describing: response))")
  27. //
  28. // let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  29. // print("responseString = \(String(describing: responseString))")
  30. // if((responseString) == "true")
  31. // {
  32. // response_token=true
  33. // }
  34. // }
  35. // task.resume()
  36. //}
  37. func checkToken()
  38. {
  39. //
  40. // Getting
  41. if let user_token = UserDefaults.standard.string(forKey: "user_token") {
  42. //print(stringOne) // Some String Value
  43. token=user_token
  44. response_token=true
  45. }
  46. }
  47. func checkIfRegistered() -> Bool
  48. {
  49. //
  50. // Getting
  51. var registered = false
  52. if(UserDefaults.standard.bool(forKey: "registered"))
  53. {
  54. registered = UserDefaults.standard.bool(forKey: "registered")
  55. }
  56. return registered
  57. }
  58. class ResearchContainerViewController: UIViewController {
  59. var contentHidden = false {
  60. didSet {
  61. guard contentHidden != oldValue && isViewLoaded else { return }
  62. children.first?.view.isHidden = contentHidden
  63. }
  64. }
  65. override func viewDidLoad() {
  66. super.viewDidLoad()
  67. let registered = checkIfRegistered()
  68. //if response_token == true {
  69. if registered == true {
  70. print("$$$$$$$Esta registrada$$$$$$$$$")
  71. performSegue(withIdentifier: "toStudy", sender: nil) }
  72. else {
  73. performSegue(withIdentifier: "toOnboarding", sender: self)
  74. }
  75. }
  76. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  77. super.prepare(for: segue, sender: sender)
  78. }
  79. @IBAction func unwindToStudy(_ segue: UIStoryboardSegue) {
  80. performSegue(withIdentifier: "toStudy", sender: self)
  81. }
  82. @IBAction func unwindToWithdrawl(_ segue: UIStoryboardSegue) {
  83. toWithdrawl()
  84. }
  85. func toWithdrawl() {
  86. let viewController = WithdrawViewController()
  87. viewController.delegate = self as? ORKTaskViewControllerDelegate
  88. present(viewController, animated: true, completion: nil)
  89. }
  90. }
  91. extension ResearchContainerViewController: ORKTaskViewControllerDelegate {
  92. public func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
  93. // Check if the user has finished the `WithdrawViewController`.
  94. if taskViewController is WithdrawViewController {
  95. if reason == .completed {
  96. performSegue(withIdentifier: "toOnboarding", sender: self)
  97. // on completion of withdrawl
  98. print("❌ User is withdrawing with string: ")
  99. print(jsonString_withdraw)
  100. sendJson_withdraw()
  101. UserDefaults.standard.set(false, forKey: "registered")
  102. UserDefaults.standard.synchronize()
  103. }
  104. dismiss(animated: true, completion: nil)
  105. }
  106. }
  107. }