I'm using Alamofire in my application and wish to display an alert if the request has an error (e.g. wrong URL), etc.
I have this function in a separate class as it is shared among the pages of the application.
Alamofire.request(.GET, api_url)
.authenticate(user: str_api_username, password: str_api_password)
.validate(statusCode: 200..<300)
.response { (request, response, data, error) in
if (error != nil) {
let alertController = UIAlertController(title: "Server Alert", message: "Could not connect to API!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
As Alamofire works asynchronously I need to do the error check then & there (unless you suggest otherwise) because then I want to manipulate the results and if the URL was wrong then it can get messy.
No surprise, the
self.presentViewController(alertController, animated: true, completion: nil)
does not work so how can I display this alert?