2016-12-26 140 views
0

目前正在使用HTTP POST方法與服務器通信的iOS應用程序中工作。獲取用戶名和密碼,如果正確與否是成功的,但服務器返回的字符串 例如:Swift HTTP POST登錄驗證

如果正確的用戶名和密碼:

<字符串>正確< /字符串>

如果不正確的用戶名或密碼:

<字符串> 0不正確/字符串>

如何添加驗證,如: 如果當前用戶名和密碼是正確的轉到下一個視圖控制器,但是如果當前用戶名或密碼不正確顯示錯誤消息而不去下一個視圖控制器。

這是我使用的代碼:

import UIKit 


class ViewController: UIViewController { 
    @IBOutlet var passwordField: UITextField! 
    @IBOutlet var usernameField: UITextField! 


override func viewDidLoad() { 
     super.viewDidLoad() 

      } 

    @IBAction func LoginGetData(_ sender: Any) { 


     var request = URLRequest(url: URL(string: "http://mylinkinhere")!) 
     request.httpMethod = "POST" 
     let postString = "username=\(usernameField.text!)&password=\(passwordField.text!)" 
     request.httpBody = postString.data(using: .utf8) 
     let task = URLSession.shared.dataTask(with: request) { data, response, error in 
      guard let data = data, error == nil else { 
       print("error=\(error)") 
       return 
      } 

      let httpStatus = response as? HTTPURLResponse 
       print("statusCode should be 200, but is \(httpStatus!.statusCode)") 
       print("response = \(response)") 
       print(postString) 

      let responseString = String(data: data, encoding: .utf8) 
      print("responseString = \(responseString)") 

     } 
     task.resume() 
    } 
} 

謝謝。

回答

2

你的API響應application/xml不是application/json,但這種反應可以使用contains和檢查是正確還是不正確。所以得到迴應後,嘗試像這樣。

let responseString = String(data: data, encoding: .utf8) 
print("responseString = \(responseString)") 

if (responseString?.contains("Incorrect")) { 
    DispatchQueue.main.async { 
     print("incorrect - try again") 
     let alert = UIAlertController(title: "Try Again", message: "Username or Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)      
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))           
     self.present(alert, animated: true, completion: nil) 
    } 
}     
else { 
    DispatchQueue.main.async { 
     print("correct good") 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: "correctone") 
     self.present(controller, animated: true, completion: nil) 
    } 
} 
+0

是的,將更新爲詳細的答案。 –

+0

@ AmdAl-Haddad歡迎隊友:) –

+0

@ AmdAl-Haddad不明白你的評論,你能指定你正在談論的細節嗎? –

0

獲取String變量中的<string> Correct </string>值。

當您收到響應字符串時,請使用此代碼。

讓STR_STATUS = VALUEFROMstring

現在,

if self.Selected == "Correct" 
{ 
    self.performSegueWithIdentifier("YOUR_SEGUE_IDENTIFIER_IN_STORYBOARD", sender: self) 
} 
else 
{ 
let alert = UIAlertController(title: "", message: "UserName Password are incorrect.", preferredStyle: UIAlertControllerStyle.Alert) 

     if handler == nil 
     { 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     } 
     else 
     { 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler)) 
     } 

     delegate.presentViewController(alert, animated: true, completion: nil) 

} 
0

請創建服務器api與數據驗證是否用戶名和相應的密碼匹配或不和發送到每一個崗位與在鑰匙內置相應的消息的響應,然後檢查響應,並提供給用戶,以及。