dongna2498 2017-02-14 02:42
浏览 50

[Swift]尝试使用我的登录按钮时,我需要在与mySQL Server通信后按两次

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()

}

}

  • 写回答

1条回答 默认 最新

  • duanhanzi8328 2017-02-14 03:10
    关注

    You need to handle the response from webservice inside the completion handler, as it stands, its trying to handle the response from the webservice before the webservice has returned.

        //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)")
    
            if responseString.range(of: "true") != nil{
                DispatchQueue.main.async {
                    var loginSwitch = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
                    var appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.window?.rootViewController = loginSwitch
                }
            }
        }
        task.resume()
    
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题