【发布时间】:2014-11-23 13:56:56
【问题描述】:
我在我的应用程序中使用 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)
不起作用,我该如何显示此警报?
【问题讨论】:
-
你能做到吗?我也遇到了同样的问题...您如何处理应用程序的错误?
标签: ios swift uialertview alamofire