Last active
June 6, 2017 19:41
-
-
Save patricklynch/b0bc705f31d411cf33abf1b93a55ed14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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