iOS:AFNetworking:如果令牌过期,则重复一个块

时间:2013-06-19 11:12:00

标签: iphone ios objective-c-blocks token afnetworking

我正在撰写社交网络应用,用户可以关注其他用户及其活动。

在服务器端,每个用户都会被标识为令牌,并在60分钟后过期。

如果令牌已过期,并且用户想要调用方法- (void) followUserWithID:(NSNumber *)targetUserID,我会使用此方法首先调用 autologinMethod (以确保用户的令牌现在有效)然后重复- (void) followUserWithID:(NSNumber *)targetUserID

注意:我不希望发出启动额外HTTP请求的“checkValidToken”请求。

-(void)commandWithParams:(NSMutableDictionary*)params command:(NSString *)command onCompletion:(JSONResponseBlock)completionBlock
{

    NSString *_path = [NSString stringWithFormat:@"%@%@",self.baseURL, command];
    NSLog(@"path: %@", _path );

    NSMutableURLRequest *apiRequest =
    [self multipartFormRequestWithMethod:@"POST"
                                    path:_path
                              parameters:params
               constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                   //TODO: attach file if needed
               }];


    AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success!
        NSLog(@"%@",responseObject);

        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //failure :(
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"ERROR"]);
        // Unable to establish a connection to the server.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server error"
                                                        message:@"Please try again later"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }];

    [operation start];
}



- (void)followUserWithID:(NSNumber *)targetUserID
{
    NSNumber *ownID = [[NSUserDefaults standardUserDefaults] objectForKey:@"id"];
    NSMutableDictionary *HTTPPostDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                               ownID, @"target_user_id",
                                               targetUserID, @"user_id",nil];

    [[WebAPI sharedInstance] commandWithParams:HTTPPostDictionary command:@"follow_user" onCompletion:^(NSDictionary *json){
        NSLog(@"%@", json);
    }];
}

1 个答案:

答案 0 :(得分:2)

你需要

  1. 检查令牌在AFNetworking完成块中是否有效
  2. 如果令牌已过期,请将其续订,然后重试该操作
  3. 根据您的服务器在这种情况下提供的HTTP状态代码,您的支票将位于successfailure区块中。

    这是一个粗略的概述:

    if (/* the token has expired */) {
        AFHTTPRequestOperation *operationToRetryAfterTokenRenewal = [operation copy];
    
        //TODO: set the completion blocks for operationToRetryAfterTokenRenewal.
    
        [myTokenRenewer autologinMethodWithCompletionBlock:^{
                         [operationToRetryAfterTokenRenewal start];
                     }];
    
    
    }
    

    两个注释:

    1. 请注意TODO。复制AFHTTPRequestOperation对象时,完成块不会保留,因此您需要再次设置它们。 (有关详细信息,请参阅AFURLConnectionOperation NSCopying Caveats。)
    2. 您应该使用[[myHTTPClient sharedClient] enqueueHTTPRequestOperation:operation]而不是[operation start],特别是如果您要进行文件上传。这允许系统一次控制运行的操作数,并在网络可达性暂时暂停时延迟运行它们。