ios - 重新运行失败的 NSURLSessionTask

标签 ios afnetworking-2 nsurlsession

我想知道是否可以重新运行失败的 NSURLSessionDataTask。这是我遇到此问题的背景。

我有一个 Web 服务对象,它使用 AFNetworking 2.0 来处理请求。在我的一种方法中,我得到了这个:

[HTTPSessionManager GET:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    //sometimes we fail here due to a 401 and have to re-authenticate.
}];

现在,有时 GET 请求会失败,因为我后端的用户身份验证 token 是 .所以我想做的是运行一个重新验证 block ,看看我们是否可以再次登录。像这样:

[HTTPSessionManager GET:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    if (task.response.statusCode == 401)
       RunReAuthenticationBlockWithSuccess:^(BOOL success) {
           //somehow re-run 'task'
        } failure:^{}
}];

有没有办法重新开始任务?

谢谢!

最佳答案

如果有人仍然感兴趣,我最终通过子类化 HTTPSessionManager 来实现我的解决方案,如下所示:

typedef void(^AuthenticationCallback)(NSString *updatedAuthToken, NSError *error);
typedef void(^AuthenticationBlock)(AuthenticationCallback);


@interface MYHTTPSessionManager : AFHTTPSessionManager
@property (nonatomic, copy) AuthenticationBlock authenticationBlock;
@end

@implementation MYHTTPSessionManager


- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler {

void (^callback)(NSString *, NSError *) = ^(NSString *tokenString, NSError *error) {
    if (tokenString) {
        //ugh...the request here needs to be re-serialized. Can't think of another way to do this
        NSMutableURLRequest *mutableRequest = [request mutableCopy];
        [mutableRequest addValue:AuthorizationHeaderValueWithTokenString(tokenString) forHTTPHeaderField:@"Authorization"];
        NSURLSessionDataTask *task = [super dataTaskWithRequest:[mutableRequest copy] completionHandler:completionHandler];
        [task resume];
    } else {
        completionHandler(nil, nil, error);
    }
};

void (^reauthCompletion)(NSURLResponse *, id, NSError *) = ^(NSURLResponse *response, id responseObject, NSError *error){

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSInteger unauthenticated = 401;
    if (httpResponse.statusCode == unauthenticated) {
        if (self.authenticationBlock != NULL) {
            self.authenticationBlock(callback); return;
        }
    }

    completionHandler(response, responseObject, error);
};

return [super dataTaskWithRequest:request completionHandler:reauthCompletion];
}

@end

关于ios - 重新运行失败的 NSURLSessionTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617294/

相关文章:

ios - NSURLSessionUploadTask 作为单独的方法,但将数据返回给调用

objective-c - 删除 Omniture 实例的 pageName 变量

android - 表 td 显示 :block on Apple devices will not work

ios - CocoaPods AFnetworkign 2.0 安装问题

ios - 使用 AFNetworking 加载离线缓存的 JSON

swift - 如何在 NSURL 中使用特殊字符?

ios NSURLSession(默认配置)不缓存请求

ios - 如何使用 GCD 说明后台任务?

android - 在 iOS 中管理核心对象

go - AFNetworking 和授权 header