0

I am quite new to swift so sorry if it is a stupid question but I am unable to find out a solution myself.

I am trying to check cardinals on application start, and show an alert.

my approach:

var apiRestManager = ApiRestManager()

override func viewDidAppear(_ animated: Bool) {
        let defaults = UserDefaults.standard
        if (defaults.value(forKeyPath: "testApp").debugDescription.characters.count > 2){
            let usernamePassword = defaults.string(forKey: "testApp")
            testCredinals(credinals: usernamePassword)
        }

    }
func testCredinals(credinals: String){
        let isLoginCorrect: Bool
        isLoginCorrect = apiRestManager.checkLogin(credinals: credinals)
        if (isLoginCorrect){
            showAlert(message: "ok")
        }
        else{
            showAlert(message: "not ok")
        }

    }
func showAlert(message: String){
        let alertController = UIAlertController(title: "iOScreator", message:
            "response " + message, preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))

        self.present(alertController, animated: true, completion: nil)


    }

and my ApiRestManager.swift :

class ApiRestManager {

    let apiURL = "http://localhost/api/"
    let checkLoginMethod = "checklogin"

    func checkLogin(credinals: String) -> Bool {
        let loginData = credinals.data(using: String.Encoding.utf8)
        let base64LoginString = loginData?.base64EncodedString()
        let url = URL (string: apiURL + checkLoginMethod)
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        var responseResult: Bool
        responseResult = false
        request.setValue("Basic " + base64LoginString!, forHTTPHeaderField: "Authorization")
        let task = URLSession.shared.dataTask(with: request) { data, response, error in

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {

                responseResult = true

            }
        }
        task.resume()


        return responseResult;
    }
}

when I am debugging the application, the alert with message not ok is displayed even before calling the API. How can I check the response and then conditionally show the alert?

Eeshwar
  • 277
  • 3
  • 17
  • Your testCredinals method is called as soon as the view is displayed. Is that expected? – Bijington Nov 09 '16 at 09:46
  • The network operation in `checkLogin` will complete asynchronously on another thread, so execution continues with the next line, which checks the Boolean, but this hasn't been set yet. You need to check the Boolean in a closure that is passed to `checkLogin` – Paulw11 Nov 09 '16 at 09:48
  • Man you really need to read about handling network request and finishing closure before separate everything into multi classes. Basically your flow: call network request -> executing in background -> showAlert -> finish executing. And the correct flow: call network request -> executing in background -> finish executing -> showAlert – Tj3n Nov 09 '16 at 09:49
  • @Tj3n has it covered! Can't believe I missed that! – Bijington Nov 09 '16 at 10:32

0 Answers0