Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
  2.  
  3. if let arr = json?["communities"] as? [[String:String]] {
  4. self.communitiesArray = arr.flatMap { $0["name"]! }
  5. }
  6. print ("first test: ",self.communitiesArray) // This prints values OK.
  7.  
  8. override func prepare(for segue: UIStoryboardSegue, sender: Any?)
  9. {
  10. if segue.identifier == "loginView" {
  11. let createViewController: ViewController = segue.destination as! ViewController
  12. createViewController.communities = communitiesArray
  13. print("second test: ", communitiesArray) //this prints nothing
  14. }
  15. }
  16.  
  17. import UIKit
  18.  
  19. protocol UsernameSentDelegate {
  20. func userLoggedIn(data: String)
  21. }
  22.  
  23. class LoginViewController: UIViewController {
  24.  
  25. var delegate: UsernameSentDelegate? = nil
  26.  
  27. @IBOutlet weak var userEmailTextField: UITextField!
  28. @IBOutlet weak var userPasswordTextField: UITextField!
  29. @IBOutlet weak var displayUserName: UILabel!
  30. var communitiesArray = [String]()
  31.  
  32.  
  33. @IBAction func loginButtonTapped(_ sender: AnyObject)
  34. {
  35.  
  36. let userEmail = userEmailTextField.text;
  37. let userPassword = userPasswordTextField.text;
  38.  
  39. if (userPassword!.isEmpty || userEmail!.isEmpty) { return; }
  40.  
  41. // send user data to server side
  42.  
  43. let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/userLogin.php");
  44. var request = URLRequest(url:myUrl!);
  45. request.httpMethod = "POST";
  46. let postString = "email=(userEmail!)&password=(userPassword!)";
  47. request.httpBody = postString.data(using: String.Encoding.utf8);
  48.  
  49. let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
  50. DispatchQueue.main.async
  51. {
  52. if(error != nil)
  53. {
  54.  
  55. //Display an alert message
  56. let myAlert = UIAlertController(title: "Alert", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert);
  57. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
  58. myAlert.addAction(okAction);
  59. self.present(myAlert, animated: true, completion: nil)
  60. return
  61. }
  62.  
  63. do {
  64.  
  65. let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
  66.  
  67. if let arr = json?["communities"] as? [[String:String]] {
  68. self.communitiesArray = arr.flatMap { $0["name"]! }
  69. }
  70. print ("first test: ",self.communitiesArray) // This print correctly in debug
  71.  
  72. // retrieve login details and check to see if all ok
  73.  
  74. if let parseJSON = json {
  75.  
  76. let returnValue = parseJSON["status"] as? String
  77.  
  78. if(returnValue != "error")
  79. {
  80.  
  81. self.delegate?.userLoggedIn(data: userEmail! )
  82.  
  83. UserDefaults.set(UserDefaults.standard)(true, forKey: "isUserLoggedIn");
  84.  
  85. self.dismiss(animated: true, completion: nil)
  86.  
  87. } else {
  88. // display an alert message
  89. let userMessage = parseJSON["message"] as? String
  90. let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);
  91. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
  92. myAlert.addAction(okAction);
  93. self.present(myAlert, animated: true, completion: nil)
  94. }
  95.  
  96. }
  97. } catch
  98. {
  99. print(error)
  100. }
  101.  
  102. }
  103.  
  104. }
  105.  
  106. task.resume()
  107.  
  108. }
  109.  
  110. override func prepare(for segue: UIStoryboardSegue, sender: Any?)
  111. {
  112.  
  113. if segue.identifier == "loginView" {
  114. let createViewController: ViewController = segue.destination as! ViewController
  115. createViewController.communities = communitiesArray
  116. print("second test: ", communitiesArray) //this prints nothing
  117. }
  118.  
  119. }
  120.  
  121.  
  122. }
  123.  
  124. performSegue(withIdentifier: "loginView", sender: self.communitiesArray)
  125.  
  126. override func prepare(for segue: UIStoryboardSegue, sender: Any?)
  127. {
  128.  
  129. if segue.identifier == "loginView" {
  130. let createViewController: ViewController = segue.destination as! ViewController
  131. createViewController.communities = sender as? Array
  132.  
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement