Ios 如何在swift 3中显示特定服务器状态代码的警报?
我开发了一个应用程序,允许用户登录到服务器,并且运行良好。 现在,我想显示一个错误警报,当服务器返回statusCode:500时,该警报表示出现了问题。 此外,我还想阻止segue,使其停留在登录屏幕上,直到用户输入正确的凭据 这是我的班级:Ios 如何在swift 3中显示特定服务器状态代码的警报?,ios,swift,alamofire,Ios,Swift,Alamofire,我开发了一个应用程序,允许用户登录到服务器,并且运行良好。 现在,我想显示一个错误警报,当服务器返回statusCode:500时,该警报表示出现了问题。 此外,我还想阻止segue,使其停留在登录屏幕上,直到用户输入正确的凭据 这是我的班级: import Alamofire //A framework for handling http requests nicely import UIKit //A class which will do the login proc
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()
}
}
如果您能提供演示,我可以帮助您提供简洁的备选答案
成功登录尝试的响应
在您的登录按钮方法中替换此代码:
@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
//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
let responseObject = response.result.value as! Dictionary<String, Any>
let statusCode = responseObject["statusCode"] as! Int
print("Status Code \(statusCode)")
// manage alerts for different status codes
if statusCode == 500 {
// present alert
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)
} else if statusCode == 200 {
// 200 status code for successful login
// code to navigate to other screen
// depends on what reponse you are getting on successful login attempt
}
}
}
@iAction func登录按钮(uu发件人:ui按钮){
参数[“电子邮件”]=电子邮件?.text//从文本字段读取电子邮件
参数[“密码”]=密码?.text//从文本字段读取密码
请求(url,方法:.post,参数:参数,编码:URLEncoding.httpBody,头:令牌)。responseJSON{
(答复)在
//从服务器读取JWT身份验证令牌
如果让tokenString=response.result.value作为?字符串{
self.token[“X-Auth-token”]=tokenString
}
//检查服务器是否返回nil
让responseObject=response.result.value为!字典
将statusCode=responseObject[“statusCode”]设为!Int
打印(“状态代码\(状态代码)”)
//管理不同状态代码的警报
如果statusCode==500{
//当前警报
let alert=UIAlertController(标题:“警报”,消息:“消息”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“单击”,样式:UIAlertActionStyle.default,处理程序:nil))
self.present(警报、动画:true、完成:nil)
}如果statusCode==200,则为else{
//200成功登录的状态代码
//导航到其他屏幕的代码
//取决于您在成功登录尝试时得到的响应
}
}
}
有明确的错误说明