Advertisement
Guest User

ugly connect

a guest
Oct 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.74 KB | None | 0 0
  1. //
  2. //  LogIn.swift
  3. //  CheckDep
  4. //
  5. //  Created by Brad Marx on 10/22/17.
  6. //  Copyright © 2017 Brad Marx. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. class LogIn: UIViewController {
  13.    
  14.     @IBOutlet weak var testLabel: UILabel!
  15.     @IBOutlet weak var userNameField: UITextField!
  16.     @IBOutlet weak var passwordField: UITextField!
  17.    
  18.     override func viewDidLoad() {
  19.         super.viewDidLoad()
  20.     }
  21.  
  22.     override func didReceiveMemoryWarning() {
  23.         super.didReceiveMemoryWarning()
  24.         // Dispose of any resources that can be recreated.
  25.     }
  26.  
  27.     @IBAction func login(_ sender: UIButton) {
  28.        
  29.         // assign username and password
  30.         let userName = userNameField.text! //"username="
  31.         let password = passwordField.text! //"password="
  32.        
  33.         // set up url path
  34.         let urlPath = "INSERT YOUR URL HERE example: https://www.yourwebsite.com/banana.php"
  35.         let url = URL(string: urlPath) //authenticateUser.php
  36.         //https://api.ipify.org/?format=json")) this is a test link you can use to try and receive and parse json data
  37.        
  38.         //generate a request to send to the server
  39.         var request = URLRequest(url: url!)
  40.         request.httpMethod = "POST"
  41.        
  42.         // create post string with username and password
  43.         let postString = "username=" + userName + "&password=" + password  // change this to however your website accepts the post string
  44.         request.httpBody = postString.data(using: .utf8)
  45.        
  46.        
  47.         //
  48.         let task = URLSession.shared.dataTask(with: request) { data, response, error in
  49.             guard let data = data, error == nil else {
  50.                 // check for fundamental networking error
  51.                 print("Data empty or error=\(String(describing: error))")
  52.                 return
  53.             }
  54.            
  55.             if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 405 {
  56.                 // check for http errors
  57.                 print("statusCode should be 200, but is \(httpStatus.statusCode)")
  58.                 print("response from status code = \(String(describing: response))")
  59.             }
  60.            
  61.             // store data in two variables, if it isn't json use the responseString
  62.             let json = try? JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
  63.             let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
  64.            
  65.             // check data in the console
  66.             print("responseString = \(responseString)")
  67.             print("--------------------------------")
  68.             print(json)
  69.             print("--------------------------------")
  70.  
  71.         }
  72.         task.resume()
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement