如何从NSURLSessionDataTask获取状态代码?

时间:2021-11-27 15:15:05

In AFNetworking 3.0 some things was changed. Basicaly, there is no more operations, instead session data tasks. How can I get status code from NSURLSessionDataTask?

在AFNetworking 3.0中,一些事情发生了变化。基本上,没有更多的操作,而是会话数据任务。如何从NSURLSessionDataTask获取状态代码?

This is my block:

这是我的阻止:

    GET("users/check_email", parameters: parameters, progress: nil, success: { sessionDataTask, response in

        // here I need status code of response

        }) { sessionDataTask, error in

            completionBlock(nil, NSError(sessionDataTask: sessionDataTask))
    } 

2 个解决方案

#1


31  

The NSURLSessionDataTask has a response property that can be cast to a NSHTTPURLResponse to get the status code:

NSURLSessionDataTask有一个响应属性,可以强制转换为NSHTTPURLResponse以获取状态代码:

Swift Example:

Swift示例:

if let urlResponse = sessionDataTask.response as? NSHTTPURLResponse {
    let status = urlResponse.statusCode
}

Objective-C Example:

Objective-C示例:

NSInteger statusCode = 0;

if ([sessionDataTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)sessionDataTask.response;
    statusCode = httpResponse.statusCode;
}

#2


0  

NSURLSessionDataTask has response property, which will be populated with an object of NSHTTPURLResponse class, which has statusCode property.

NSURLSessionDataTask具有响应属性,该属性将填充NSHTTPURLResponse类的对象,该类具有statusCode属性。

#1


31  

The NSURLSessionDataTask has a response property that can be cast to a NSHTTPURLResponse to get the status code:

NSURLSessionDataTask有一个响应属性,可以强制转换为NSHTTPURLResponse以获取状态代码:

Swift Example:

Swift示例:

if let urlResponse = sessionDataTask.response as? NSHTTPURLResponse {
    let status = urlResponse.statusCode
}

Objective-C Example:

Objective-C示例:

NSInteger statusCode = 0;

if ([sessionDataTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)sessionDataTask.response;
    statusCode = httpResponse.statusCode;
}

#2


0  

NSURLSessionDataTask has response property, which will be populated with an object of NSHTTPURLResponse class, which has statusCode property.

NSURLSessionDataTask具有响应属性,该属性将填充NSHTTPURLResponse类的对象,该类具有statusCode属性。