Advertisement
Guest User

Untitled

a guest
Jan 1st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. //Getting data from database
  2. func getData() -> Void {
  3.  
  4. let url: String = "http://localhost/fridge_app/login.php" //this will be changed to the path where service.php lives
  5.  
  6. //created NSURL
  7. let requestURL = NSURL(string: url)
  8.  
  9. //creating NSMutableURLRequest
  10. var request = URLRequest(url: requestURL! as URL)
  11.  
  12. //setting the method to post
  13. request.httpMethod = "POST"
  14.  
  15.  
  16. //Getting values from textfield
  17. let usernameVal = username.text
  18. let passwordVal = password.text
  19.  
  20. //creating the post parameter by concatenating the keys and values from text field
  21. let postString = "username=(usernameVal!)&password=(passwordVal!)";
  22. print(postString)
  23. request.httpBody = postString.data(using: String.Encoding.utf8)
  24.  
  25. //creating a task to send the post request
  26. let task = URLSession.shared.dataTask(with: request as URLRequest){
  27. data, response, error in
  28.  
  29. //exiting if there is some error
  30. if error != nil{
  31. print("error is (error)")
  32. return;
  33. }
  34.  
  35. // Print out response string
  36. var responseString: NSString?;
  37. responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  38. if(responseString == "invalid"){
  39. self.isValid = false;
  40. print(self.isValid)
  41. }
  42.  
  43. if self.checkLogin(data: responseString!) == true {
  44. self.performSegue(withIdentifier: "profileViewController", sender: self)
  45. }
  46. else{
  47. print("Hello")
  48. // It prints hello fine, but when it tries to run the showAlert function it fails
  49. self.showAlert()
  50. }
  51.  
  52.  
  53. //print("responseString = (self.responseString)")
  54.  
  55. }
  56.  
  57. //executing the task
  58. task.resume()
  59. }
  60.  
  61. /*
  62. * Show UIAlert Message
  63. */
  64. func showAlert() -> Void{
  65. let alert = UIAlertController(title: "User Does Not Exist",
  66. message: "",
  67. preferredStyle: UIAlertControllerStyle.alert)
  68. let loginFail = UIAlertAction(title: "Close", style: .default, handler: nil);
  69.  
  70. alert.addAction(loginFail);
  71. present(alert, animated: true)
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement