2017-01-03 55 views
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() 
} 
} 
+0

https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md 明確指出錯誤 – karthikeyan

回答

0

我可以幫你整齊&備選答案,如果你能提供成功的登錄嘗試演示 響應。

loginButton方法替換此代碼:

@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 
     } 
    } 
} 
相關問題