2016-12-26 56 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与数据验证是否用户名和相应的密码匹配或不和发送到每一个岗位与在钥匙内置相应的消息的响应,然后检查响应,并提供给用户,以及。