Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS:AFNetworking:如果令牌过期,则重复一个块_Iphone_Ios_Objective C Blocks_Token_Afnetworking - Fatal编程技术网

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

Iphone iOS:AFNetworking:如果令牌过期,则重复一个块,iphone,ios,objective-c-blocks,token,afnetworking,Iphone,Ios,Objective C Blocks,Token,Afnetworking,我正在编写一个社交网络应用程序,用户可以在其中跟踪其他用户及其活动 在服务器端,每个用户都用一个令牌标识,该令牌在60分钟后过期 如果令牌已过期,并且用户希望调用方法-(void)followUserWithID:(NSNumber*)targetUserID,我希望此方法首先调用我的autologinMethod(以确保用户的令牌现在有效),然后重复-(void)followUserWithID:(NSNumber*)targetUserID 注意:我不希望有一个“checkValidToke

我正在编写一个社交网络应用程序,用户可以在其中跟踪其他用户及其活动

在服务器端,每个用户都用一个令牌标识,该令牌在60分钟后过期

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

注意:我不希望有一个“checkValidToken”请求启动额外的HTTP请求

-(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);
    }];
}
-(void)commandWithParams:(NSMutableDictionary*)params命令:(NSString*)命令onCompletion:(JSONResponseBlock)completionBlock
{
NSString*_path=[NSString stringWithFormat:@“%@%@”,self.baseURL,command];
NSLog(@“路径:%@”,_路径);
NSMutableURLRequest*APIRQUEST=
[self multipartFormRequestWithMethod:@“POST”
路径:_路径
参数:params
构造体WithBlock:^(id formData){
//TODO:如果需要,请附加文件
}];
AFJSONRequestOperation*操作=[[AFJSONRequestOperation alloc]initWithRequest:apiRequest];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
//成功!
NSLog(@“%@”,响应对象);
完成块(响应对象);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
//失败:(
completionBlock([NSDictionary Dictionary WithObject:[error localizedDescription]forKey:@“error”]);
//无法建立与服务器的连接。
UIAlertView*警报=[[UIAlertView alloc]initWithTitle:@“服务器错误”
消息:@“请稍后再试”
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报显示];
}];
[操作启动];
}
-(void)followUserWithID:(NSNumber*)targetUserID
{
NSNumber*ownID=[[NSUserDefaults standardUserDefaults]objectForKey:@“id”];
NSMutableDictionary*HTTPPostDictionary=[[NSMutableDictionary alloc]initWithObjectsAndKeys:
ownID,@“目标用户id”,
targetUserID,@“user_id”,nil];
[[WebAPI sharedInstance]commandWithParams:HTTPPostDictionary命令:@“follow_user”onCompletion:^(NSDictionary*json){
NSLog(@“%@”,json);
}];
}
您需要

  • 检查AFNetworking completion块中的令牌是否有效
  • 如果令牌已过期,请续订它,然后重试该操作
  • 根据服务器在本例中提供的HTTP状态代码,您的检查将在
    success
    failure
    块中进行

    这里有一个大致的轮廓:

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

  • 请注意TODO。复制
    AFHTTPRequestOperation
    对象时,完成块不会持续存在,因此需要重新设置它们。(有关更多信息,请参阅。)
  • 您应该真正使用
    [[myHTTPClient sharedClient]排队HttpRequestOperation:operation]
    而不是
    [operation start]
    ,尤其是当您要上传文件时。这允许系统控制一次运行多少操作,并在网络可达性出现临时暂停时延迟运行这些操作

  • 谢谢,这将意味着需要一个单独的服务器请求来检查我的令牌的有效性。我实际上发布了我当前的令牌,并根据其有效性转到成功或失败块。但是,我想在失败时重新执行整个操作。我不知道如何以智能方式执行此操作。我不能只调用相同的令牌操作(如递归调用)现在可以吗?您不需要单独的请求来检查有效性。如果失败块是您使用无效令牌的地方,那么请将我编写的示例代码放入失败块中。您可以通过复制
    AFHTTPRequestOperation
    对象、重设完整/失败块并启动它来重做整个操作,就像在我的代码中一样艾因。