我正在编写一个社交网络应用程序,用户可以在其中关注其他用户及其活动。
在服务器端,每个用户都使用一个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);
}];
}