ユーザーがデバイスを承認した後にのみ RESTful API にアクセスできるようにする iOS アプリ用の Rails バックエンドを構築しました。基本的に認可はトークンを取得して管理します。
ユーザーがユーザー名とパスワードを送信すると、Web サービスが呼び出されますAFHTTPRequestOperation
(以下のコードを参照)。MBProgressHUD
また、リクエストの進行状況を追跡するためにHUD ( ) をユーザーに表示します。成功と失敗のコールバックを設定しました。HUD を更新し、更新されたメッセージを画面上に数秒間表示してから、それを閉じるようにしたいと考えています。
//Set HUD
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Authenticating";
//Set HTTP Client and request
NSURL *url = [NSURL URLWithString:@"http://localhost:3000"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setParameterEncoding:AFFormURLParameterEncoding]; //setting x-www-form-urlencoded
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/api/v1/tokens.json" parameters:@{@"password":_passwordField.text, @"email":_emailField.text}];
//Set operation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//Success and failure blocks
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSError *error;
NSDictionary* jsonFromData = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@", jsonFromData);
_statusLabel.text = @"Device authenticated!";
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
hud.mode = MBProgressHUDModeCustomView;
hud.labelText = @"Authenticated!";
sleep(2);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error");
sleep(2);
_statusLabel.text = @"Wrong username or password!";
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}];
成功/失敗操作のコールバックが呼び出されると:
- HUD モードとテキストを更新しようとしました。
sleep()
数秒待ちます。- HUD を閉じ
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
ます。
dispatch_queue_t dispatch_get_main_queue(void);
また、メイン スレッドで HUD の更新を使用して実行しようとしましたが、役に立ちませんでした。
私が間違っていることについてのアイデアはありますか?