Last active
January 16, 2018 22:32
-
-
Save projectxcappe/7b422177881f92ab60ac6771e3ae5e3f 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
//MARK:API | |
func loginRequest(username:String, password:String) { | |
activityIndicator.startAnimating() | |
self.fieldView.endEditing(true) //reset screen | |
guard let url = URL(string:"http://api.lineopener.com/index.php/api/login") else { return } | |
let parameters = ["email": username, "password": password] | |
var request = URLRequest(url: url) | |
request.httpMethod = "POST" | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return } | |
request.httpBody = httpBody | |
let session = URLSession.shared | |
session.dataTask(with: request) { (data, response, error) in | |
if let response = response { | |
print(response) | |
} | |
if let data = data { | |
do { | |
// let dataAsString = String(data: data, encoding: .utf8) | |
// print(dataAsString) | |
do { | |
DispatchQueue.main.async(execute: { | |
self.activityIndicator.stopAnimating() | |
}) | |
let signin = try | |
JSONDecoder().decode(SignIn.self, from: data) | |
//need alerts on main thread | |
DispatchQueue.main.async(execute: { | |
if !signin.msg.isEmpty { | |
self.alert(msg: signin.msg) | |
} | |
}) | |
} catch let jsonErr { | |
print("Error: ", jsonErr) | |
} | |
} | |
} | |
}.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment