Skip to content

Instantly share code, notes, and snippets.

@patricklynch
Last active June 6, 2017 19:41
Show Gist options
  • Save patricklynch/b0bc705f31d411cf33abf1b93a55ed14 to your computer and use it in GitHub Desktop.
Save patricklynch/b0bc705f31d411cf33abf1b93a55ed14 to your computer and use it in GitHub Desktop.
class User {
static let URL_SAVE_TEAM = URL(string: "http://localhost/test/login_mobile.php")!
func login(email: String, password: String, completion: @escaping (String?)->()) {
var request = URLRequest(url: User.URL_SAVE_TEAM)
request.httpMethod = "POST"
let postParameters = "email=\(email)&password=\(password)"
request.httpBody = postParameters.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("error is \(error.localizedDescription)")
completion(nil)
return
}
guard let data = data else {
print("No data was returned by the request!")
completion(nil)
return
}
do {
guard let parseJSON = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Dictionary<String, String?>,
let msg = parseJSON["message"] as? String else {
print("Error parsing data")
completion(nil)
return
}
print("Responso di ritorno: \(msg)")
completion(msg)
} catch {
print("error is \(error.localizedDescription)")
completion(nil)
}
}
task.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment