1
我構建了一個應用程序,它允許用戶登錄到服務器,並且工作正常。 現在我想顯示一個錯誤提示,說當服務器返回statusCode:500時出現錯誤。 另外我想阻止這個繼續,所以它停留在登錄屏幕上,直到用戶輸入正確的憑證。如何在swift 3中顯示特定服務器狀態碼的警報?
這裏是我的類:
import Alamofire //A framework for handling http requests nicely
import UIKit
//A class which will do the login process
class InitialViewController: UIViewController {
//Variables for the URL, parameters, authentication token and alertView
let url = "https://api.sis.kemoke.net/auth/login"
var parameters = ["email": "", "password": ""]
var token = ["X-Auth-Token": ""]
// Parameters textfields
@IBOutlet weak var email: UITextField?
@IBOutlet weak var password: UITextField?
// A method for the login button
@IBAction func loginButton(_ sender: UIButton) {
parameters["email"] = email?.text //Read email from text field
parameters["password"] = password?.text //Read password from text field
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: token).responseJSON {
(response) in
print(response.result.value as Any)
//Reading JWT authentication token from the server
if let tokenString = response.result.value as? String {
self.token["X-Auth-Token"] = tokenString
}
//Check if the server returned nil
if response == nil {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
//Show the error message
//Check NSUserData if the user is already logged in
}
}
//ViewDidLoad method used to preconfigure the first view
override func viewDidLoad() {
super.viewDidLoad()
}
}
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md 明確指出錯誤 – karthikeyan