I have created this static function Login to wrap Alamofire authenticate function. I have one problem how to implementing closure with Alamofire authenticate and how to call it in action asynchronous.
class AutheManager{
let username:String = "user"
let password:String = "password"
var manager: Session!
static func Login(username:String, password:String, completion: @escaping (_ success: Bool, _ response: DataResponse<Data?>?) -> ()) {
var response:DataResponse<Data?>?
AF.request("https://httpbin.org/basic-auth/\(username)/\(password)")
.authenticate(username: username, password: password)
.response { resp in
response = resp
completion(true,response)
}
if(response?.response?.statusCode == 200)
{
completion(true, response)
}
else {
completion(false, nil)
}
}
}
//code from action outlet
@IBAction func loginAction(sender: UIButton)
{
AutheManager.Login(username: newAccountName, password: newPassword) { (success, response) in
if (success == true)
{
// rest of code
}
else
{
}
}
}