简体   繁体   中英

PushViewController doesn't work after httpPost

I have made a login screen which takes the input and communicates with the REST api to verify the user. If the response is true, I login the user else not. I have written a method openViewControllerBasedOnIdentifier(id) to switch views.

The REST api returns true and false appropriately. The push controller gets called but view does not change. How if I place only one line in LoginAction method 'self.openViewControllerBasedOnIdentifier("PlayVC")' and remove the rest of the code , it works fine.

Here is my code @IBAction func LoginAction(_ sender: Any) {

    //self.openViewControllerBasedOnIdentifier("PlayVC")

Constants.login_status = false
    //created NSURL
    let requestURL = NSURL(string: URL_BK)

    //creating NSMutableURLRequest
    let request = NSMutableURLRequest(url: requestURL! as URL)

    //setting the method to post
    request.httpMethod = "POST"
    let username = phonenumber.text

    //creating the post parameter by concatenating the keys and values from text field
    let postParameters = "username="+username!+"&password=bk&schoolId=0";

    //adding the parameters to request body
    request.httpBody = postParameters.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
        let responseData = String(data: data!, encoding: String.Encoding.utf8)
        if error != nil{
            print("error is \(error)")
            return;
        }

        //parsing the response
        do {

            print(“Received data is ---%@",responseData as Any)

            let myJSON =  try JSONSerialization.jsonObject(with: data! , options: .allowFragments) as? NSDictionary


            if let parseJSON = myJSON {


                var status : Bool!

                status = parseJSON["status"] as! Bool?
                //print(status)

                if status==false
                {
                    Constants.login_status = false

                                        }
                else{
                    Constants.login_status = true
                    print("calling PLAYVC")

                    self.openViewControllerBasedOnIdentifier("PlayVC")


                }

            }
            else{
                print("NULL VALUE RECEIVED")
            }


        } catch {
            print(error)
        }

    }
    //executing the task
    task.resume()




}

You should open the new view controller on the main thread like this:

DispatchQueue.main.async {
    self.openViewControllerBasedOnIdentifier("PlayVC")
}

Your REST API query response is processed in a background thread when you call URLSession.shared.dataTask and so when you call any UI actions, you should wrap the code as above to execute the UI code in the main thread. Then it would work fine :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM