Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LogIn.swift
- // CheckDep
- //
- // Created by Brad Marx on 10/22/17.
- // Copyright © 2017 Brad Marx. All rights reserved.
- //
- import UIKit
- import Foundation
- class LogIn: UIViewController {
- @IBOutlet weak var testLabel: UILabel!
- @IBOutlet weak var userNameField: UITextField!
- @IBOutlet weak var passwordField: UITextField!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func login(_ sender: UIButton) {
- // assign username and password
- let userName = userNameField.text! //"username="
- let password = passwordField.text! //"password="
- // set up url path
- let urlPath = "INSERT YOUR URL HERE example: https://www.yourwebsite.com/banana.php"
- let url = URL(string: urlPath) //authenticateUser.php
- //https://api.ipify.org/?format=json")) this is a test link you can use to try and receive and parse json data
- //generate a request to send to the server
- var request = URLRequest(url: url!)
- request.httpMethod = "POST"
- // create post string with username and password
- let postString = "username=" + userName + "&password=" + password // change this to however your website accepts the post string
- request.httpBody = postString.data(using: .utf8)
- //
- let task = URLSession.shared.dataTask(with: request) { data, response, error in
- guard let data = data, error == nil else {
- // check for fundamental networking error
- print("Data empty or error=\(String(describing: error))")
- return
- }
- if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 405 {
- // check for http errors
- print("statusCode should be 200, but is \(httpStatus.statusCode)")
- print("response from status code = \(String(describing: response))")
- }
- // store data in two variables, if it isn't json use the responseString
- let json = try? JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
- let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
- // check data in the console
- print("responseString = \(responseString)")
- print("--------------------------------")
- print(json)
- print("--------------------------------")
- }
- task.resume()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement