Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet var passwordField: UITextField!
- @IBOutlet var usernameField: UITextField!
- @IBAction func LoginButton(_ sender: Any) {
- var request = URLRequest(url: URL(string: "http link here.")!)
- 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)")
- 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)
- }
- }
- }
- task.resume()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement