daily pastebin goal
61%
SHARE
TWEET

ios http post

wesare Dec 29th, 2016 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2.  
  3.  
  4. class ViewController: UIViewController {
  5.     @IBOutlet var passwordField: UITextField!
  6.     @IBOutlet var usernameField: UITextField!
  7.    
  8.     @IBAction func LoginButton(_ sender: Any) {
  9.         var request = URLRequest(url: URL(string: "http link here.")!)
  10.         request.httpMethod = "POST"
  11.         let postString = "username=\(usernameField.text!)&password=\(passwordField.text!)"
  12.         request.httpBody = postString.data(using: .utf8)
  13.         let task = URLSession.shared.dataTask(with: request) { data, response, error in
  14.             guard let data = data, error == nil else {
  15.                 print("error=\(error)")
  16.                 return
  17.             }
  18.            
  19.             let httpStatus = response as? HTTPURLResponse
  20.             print("statusCode should be 200, but is \(httpStatus!.statusCode)")
  21.             print("response = \(response)")
  22.             print(postString)
  23.            
  24.             let responseString = String(data: data, encoding: .utf8)
  25.             print("responseString = \(responseString)")
  26.            
  27.             if  (responseString?.contains("Incorrect"))!{
  28.                 DispatchQueue.main.async {
  29.                     print("incorrect - try again")
  30.                     let alert = UIAlertController(title: "Try Again", message: "Username or Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)
  31.                    
  32.                     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  33.                    
  34.                    
  35.                     self.present(alert, animated: true, completion: nil)
  36.                 }
  37.             }
  38.                
  39.             else{
  40.                 DispatchQueue.main.async {
  41.                     print("correct good")
  42.  
  43.                     let storyboard = UIStoryboard(name: "Main", bundle: nil)
  44.                     let controller = storyboard.instantiateViewController(withIdentifier: "correctone")
  45.                     self.present(controller, animated: true, completion: nil)
  46.                 }
  47.             }
  48.         }
  49.        
  50.         task.resume()
  51.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top