Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Created May 10, 2018 07:59
Show Gist options
  • Select an option

  • Save riccardopirani/6060cdd993e1c6297753ef150eaee875 to your computer and use it in GitHub Desktop.

Select an option

Save riccardopirani/6060cdd993e1c6297753ef150eaee875 to your computer and use it in GitHub Desktop.
Enable https on Swift
struct Database{
let port=":5656"
let server="https://localhost"
func GetServerURL()->String{
return server+""+port
}
}
func Login(Username: String, Password: String, completion: @escaping (Int) -> ())
{
let semaphore = DispatchSemaphore(value: 1)
semaphore.wait()
let db = Database()
let json: [String: Any] = ["Username": "" + Username, "Password": "" + Password]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
var request = URLRequest(url: URL(string: db.GetServerURL() + "/login")!)
request.httpMethod = "POST"
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
completion(0)
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
let read = responseJSON["return"]!
let IdUser=Int(String(describing: read))!
if (IdUser > 0) {
completion(IdUser)
}
else {
completion(0)
}
}
}
task.resume()
semaphore.signal()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment