I am creating an application that uses a login and register page. After a user registers their account it is in my mySQL database. So when I go to test the login, I will press login, then nothing happens, however I am receiving a message from my php saying "success" but the application will not segue until I click on the Login button again. I thought that maybe my if statement was in the wrong location, but I can't figure out where to move it because my task.resume is locked to where it is, and the if statement needs to execute with a button press. I feel like this will be an easy problem to fix, but for the life of me I cannot figure it out. All help is appreciated, this is my loginViewController.swift file.(I removed my POST website on purpose)
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
class loginViewController: UIViewController {
struct globalVariable {
static var response = String()
}
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBAction func loginButtonTap(_ sender: Any) {
do {
let request = try NSMutableURLRequest(url: NSURL(string: "")! as URL)
request.httpMethod = "POST"
//Getting the values from the text fields and creating the post parameter
let postString = "email=\(emailTextField.text!)&password=\(passwordTextField.text!)"
//Adding the parameters to request body
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
if error != nil{
print("error is=\(error)")
return;
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
globalVariable.response = responseString as! String
}
task.resume()
if globalVariable.response.range(of: "true") != nil{
var loginSwitch = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
var appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginSwitch
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}