Swift保存PHP会话以保持身份验证

时间:2018-06-01 17:22:34

标签: php ios swift

我正在构建一个应用程序,其登录页面使用PHP和MySQL与我的AWS服务器连接,一切正常,但我不知道如何在应用程序中保存PHP会话,以便用户可以保持身份验证。< / p>

我的部分PHP代码:

<?php
$session_start();
...
...
if($verify){
    echo "Verfied";
    $_SESSION["Authenticated"] = 1;
  } else {
    echo "Unverified";
    $_SESSION["Authenticated"] = 0;
  }
...
...
?>

Swift代码的一部分:

let url = URL(string: "http://localhost/login.php")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "username=\(username)&password=\(password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error == nil else {                                                 // check for fundamental networking error
        print("error=\(String(describing: error))")
        return
    }

    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
        print("statusCode should be 200, but is \(httpStatus.statusCode)")
        print("response = \(String(describing: response))")
    }

    let responseString = String(data: data, encoding: .utf8)!
    print("responseString = \(String(describing: responseString))")
    DispatchQueue.main.async() {
        if (responseString == "Vaild!") {
            self.performSegue(withIdentifier: "MainPageSegue", sender: self)
        } else {
            self.errorLabel.text = "Invalid username or password."
        }
    }
}
task.resume()

如何在我的应用中保存会话,以便用户保持身份验证?

0 个答案:

没有答案