Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class LoginViewController: UIViewController {
- @IBOutlet weak var userUsernameTextField: UITextField!
- @IBOutlet weak var userPasswordTextField: UITextField!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func loginButtonTapped(_ sender: Any)
- {
- let userUsername = userUsernameTextField.text
- let userPassword = userPasswordTextField.text
- if(userUsername!.isEmpty || userPassword!.isEmpty)
- {
- // Display an alert message
- let myAlert = UIAlertController(title: "Alert", message:"All fields are required to fill in", preferredStyle: UIAlertControllerStyle.alert);
- let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
- myAlert.addAction(okAction);
- self.present(myAlert, animated: true, completion: nil)
- return
- }
- let myUrl = URL(string: "http://cgi.soic.indiana.edu/~team10/userLogin.php");
- var request = URLRequest(url:myUrl!);
- request.httpMethod = "POST";
- request.addValue("application/json", forHTTPHeaderField: "Content-Type")
- let postString = "username=(userUsername!)&password=(userPassword!)";
- request.httpBody = postString.data(using: String.Encoding.utf8);
- let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
- DispatchQueue.main.async
- {
- if(error != nil)
- {
- //Display an alert message
- let myAlert = UIAlertController(title: "Alert", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert);
- let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
- myAlert.addAction(okAction);
- self.present(myAlert, animated: true, completion: nil)
- return
- }
- do {
- let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
- if let parseJSON = json {
- let userId = parseJSON["userId"] as? String
- if(userId != nil)
- {
- // take user to a protected page
- } else {
- // display an alert message
- let userMessage = parseJSON["message"] as? String
- let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);
- let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
- myAlert.addAction(okAction);
- self.present(myAlert, animated: true, completion: nil)
- }
- }
- } catch
- {
- print(error)
- }
- }
- }
- task.resume()
- }
- }
- let postString = "username=(userUsername!)&password=(userPassword!)"
- let parameters = ["username":userUsername!,"password":userPassword!]
- do{
- let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
- request.httpBody = data
- }
- catch{
- print(error)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement