在NSURLSessionDataTask
有response
可转换为一个属性NSHTTPURLResponse
来获取状态代码:
Swift示例:
if let urlResponse = sessionDataTask.response as? NSHTTPURLResponse { let status = urlResponse.statusCode }
Objective-C示例:
NSInteger statusCode = 0; if ([sessionDataTask.response isKindOfClass:[NSHTTPURLResponse class]]) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)sessionDataTask.response; statusCode = httpResponse.statusCode; }