我在我的应用程序中使用 Alamofire 并希望在请求有错误(例如错误的 URL)等时显示警报。
我在一个单独的类中有这个函数,因为它在应用程序的页面之间共享。
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)
}
}
由于 Alamofire 异步工作,我需要在那时和那里进行错误检查(除非你另有建议),因为那时我想操纵结果,如果 URL 错误,那么它可能会变得混乱。
毫不奇怪,
self.presentViewController(alertController, animated: true, completion: nil)
不起作用,那么如何显示此警报?