使用AFNetWorking请求数据时,有时并不能直接拿到错误code和信息,如需获取可以使用以下方式:
AFN新版本(>4.0.1)
// 获取错误信息mutableUserInfo
NSError *mutableUserInfo = error.userInfo[NSUnderlyingErrorKey];
// 获取错误响应请求信息
NSHTTPURLResponse *urlResponse = mutableUserInfo.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];
// 获取错误响应信息
NSString *errStr = [[NSString alloc] initWithData: mutableUserInfo.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding: NSUTF8StringEncoding];
if (urlResponse.statusCode == 302) {
// 状态为302
// TODO
}
AFN旧版本
// 通过error信息进行获取
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:0 error:nil];
// 错误码
response[@"status”]
// 错误消息
response[@"message"]