ユーザーが他のユーザーとそのアクティビティをフォローできるソーシャル ネットワーキング アプリを作成しています。
サーバー側では、各ユーザーは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);
}];
}