Guest User

Untitled

a guest
Apr 30th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. import UIKit
  2.  
  3. class SigninViewController: UIViewController {
  4.  
  5. @IBOutlet weak var userNameTextFeild: UITextField!
  6. @IBOutlet weak var userPasswordTextField: UITextField!
  7.  
  8. @IBAction func loginInButtonTapped(_ sender: Any) {
  9. print("log in button tapped")
  10.  
  11. //Read values from text field
  12. let userName = userNameTextFeild.text
  13. let userPassword = userPasswordTextField.text
  14.  
  15. // Check if required fields are not empty
  16. if (userName?.isEmpty)! || (userPassword?.isEmpty)! {
  17. // Display alert message here
  18. print("User name (String(describing: userName)) or password (String(describing: userPassword)) is empty")
  19. displayMessage(userMessage: "One of the required fields is missing")
  20.  
  21. return
  22. }
  23.  
  24. //Create Activity Indicator
  25. let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
  26.  
  27. // Position Activity Indicator in the center of the main view
  28. myActivityIndicator.center = view.center
  29.  
  30. // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
  31. myActivityIndicator.hidesWhenStopped = false
  32.  
  33. // Start Activity Indicator
  34. myActivityIndicator.startAnimating()
  35.  
  36. view.addSubview(myActivityIndicator)
  37.  
  38. //Send HTTP Request to perform Sign in
  39. let myUrl = URL(string: "http://198.162.8.80:50000/login/")
  40. var request = URLRequest(url:myUrl!)
  41. request.httpMethod = "POST"// Compose a query string
  42. request.addValue("application/json", forHTTPHeaderField: "content-type")
  43. request.addValue("application/json", forHTTPHeaderField: "Accept")
  44.  
  45. let postString = ["userName": userName!, "userPassword": userPassword!] as [String: String]
  46.  
  47. do {
  48. request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted)
  49. } catch let error {
  50. print(error.localizedDescription)
  51. displayMessage(userMessage: "Something went wrong...")
  52. return
  53. }
  54.  
  55. let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
  56. self.removeActivityIndicator(activityIndicator: myActivityIndicator)
  57.  
  58. if error != nil {
  59. self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later")
  60. print("error=(String(describing: error))")
  61. return
  62. }
  63.  
  64. //Let's convert response sent from a server side code to a NSDictionary object:
  65. do {
  66. let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
  67.  
  68. if let parseJSON = json {
  69.  
  70. if parseJSON["errorMessageKey"] != nil {
  71. self.displayMessage(userMessage: parseJSON["errorMessage"] as! String)
  72. return
  73. }
  74.  
  75. DispatchQueue.main.async {
  76. let homePage = self.storyboard?.instantiateViewController(withIdentifier: "dashboardViewController") as! dashboardViewController
  77. let appDelegate = UIApplication.shared.delegate
  78. appDelegate?.window??.rootViewController = homePage
  79. }
  80. } else {
  81. //Display an Alert dialog with a friendly error message
  82. self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later")
  83. }
  84.  
  85. } catch {
  86. self.removeActivityIndicator(activityIndicator: myActivityIndicator)
  87.  
  88. // Display an Alert dialog with a friendly error message
  89. self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later")
  90. print(error)
  91. }
  92. }
  93. task.resume()
  94. }
  95.  
  96. @IBAction func registerNewAccountButtonTapped(_ sender: Any) {
  97. print("Register account button tapped")
  98.  
  99. let registerViewController = self.storyboard?.instantiateViewController(withIdentifier: "registerViewController") as! registerViewController
  100. self.present(registerViewController, animated: true)
  101. }
  102.  
  103. func displayMessage(userMessage:String) -> Void {
  104. DispatchQueue.main.async {
  105. let alertController = UIAlertController(title: "Alert", message: userMessage, preferredStyle: .alert)
  106. let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
  107. // Code in this block will trigger when OK button tapped.
  108. print("Ok button tapped")
  109. DispatchQueue.main.async {
  110. self.dismiss(animated: true, completion: nil)
  111. }
  112. }
  113. alertController.addAction(OKAction)
  114. self.present(alertController, animated: true, completion:nil)
  115. }
  116. }
  117.  
  118. func removeActivityIndicator(activityIndicator: UIActivityIndicatorView) {
  119. DispatchQueue.main.async{
  120. activityIndicator.stopAnimating()
  121. activityIndicator.removeFromSuperview()
  122. }
  123. }
  124. }
Add Comment
Please, Sign In to add comment