1
ユーザーがサーバーにログインして正常に動作するようにするアプリを構築しました。 サーバーがstatusCode:500を返すときに何かが間違っているというエラー警告を表示したいとします。 また、ユーザーが正しい資格情報を入力するまで、ログイン画面に表示されるようにセグを防ぎます。あなたが成功したログイン試行のためのデモ 応答を提供することができれば、私はきちんと&代替の答えであなたを助けることができる迅速な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