Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Getting data from database
- func getData() -> Void {
- let url: String = "http://localhost/fridge_app/login.php" //this will be changed to the path where service.php lives
- //created NSURL
- let requestURL = NSURL(string: url)
- //creating NSMutableURLRequest
- var request = URLRequest(url: requestURL! as URL)
- //setting the method to post
- request.httpMethod = "POST"
- //Getting values from textfield
- let usernameVal = username.text
- let passwordVal = password.text
- //creating the post parameter by concatenating the keys and values from text field
- let postString = "username=(usernameVal!)&password=(passwordVal!)";
- print(postString)
- request.httpBody = postString.data(using: String.Encoding.utf8)
- //creating a task to send the post request
- let task = URLSession.shared.dataTask(with: request as URLRequest){
- data, response, error in
- //exiting if there is some error
- if error != nil{
- print("error is (error)")
- return;
- }
- // Print out response string
- var responseString: NSString?;
- responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
- if(responseString == "invalid"){
- self.isValid = false;
- print(self.isValid)
- }
- if self.checkLogin(data: responseString!) == true {
- self.performSegue(withIdentifier: "profileViewController", sender: self)
- }
- else{
- print("Hello")
- // It prints hello fine, but when it tries to run the showAlert function it fails
- self.showAlert()
- }
- //print("responseString = (self.responseString)")
- }
- //executing the task
- task.resume()
- }
- /*
- * Show UIAlert Message
- */
- func showAlert() -> Void{
- let alert = UIAlertController(title: "User Does Not Exist",
- message: "",
- preferredStyle: UIAlertControllerStyle.alert)
- let loginFail = UIAlertAction(title: "Close", style: .default, handler: nil);
- alert.addAction(loginFail);
- present(alert, animated: true)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement