1

AFNetworkingLibraryを使用したプロジェクトを作成しています。これを使用して、Webサービスでログインとパスワードを確認します。これで問題がなければ200コードが返されます。200の場合、アプリケーションを続行できるようにブール値をtrueに設定します。しかし、このブール値は適切なタイミングで設定されていません。それが機能する前に、私は常に自分を2回押す必要がありlogin buttonます。これがブール値を設定するための私のコードです。

    - (BOOL)credentialsValidated {
    self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.progressHUD.labelText = @"Loading";
    self.progressHUD.mode = MBProgressHUDModeIndeterminate;
    self.progressHUD.dimBackground = YES;
     [[API sharedInstance] loginCommand:[NSMutableDictionary dictionaryWithObjectsAndKeys:_txtLogin.text,@"email",_txtPass.text,@"pwd", nil] onCompletion:^(NSDictionary *json){
     //completion
         if(![json objectForKey:@"error"]){
             NSLog(@"status %@",[json valueForKeyPath:@"data.status"]);
             if([[json valueForKeyPath:@"data.status"]intValue] == 200){
                //Create user object


                _loginstatus =  YES;
                [self.progressHUD hide:YES afterDelay:5];
             }else{
                 //show validation
                 _txtLogin.text = @"";
                 _txtPass.text = @"";
                 _loginstatus = NO;
             }
         }else {
             NSLog(@"Cannot connect to the server");
         }
     }];
     if(_loginstatus){
         NSLog(@"true");
    }else{
        NSLog(@"false");
    }
    [self.progressHUD hide:YES afterDelay:5];
     return _loginstatus;
}

そして、これが私のAPIコードです。

-(void)loginCommand:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock{
    NSLog(@"%@%@",kAPIHost,kAPILogin);
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPILogin 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(@"SUCCESSSS!");
        completionBlock(responseObject);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        //Failure
        NSLog(@"FAILUREE!");
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];
    [operation start];

}

[operation start]コードの下に置く[operation waitUntilFinish]と、アプリがクラッシュします。

誰かがこれを手伝ってくれますか?

敬具

ログこれは、ログインボタンを2回押した後 のログの印刷です。

2012-12-28 09:36:00.547 Offitel[6532:907] http://virtuele-receptie.******.sanmax.be/nl/webservice/company-user/****/*****/**************
2012-12-28 09:36:00.561 Offitel[6532:907] false
2012-12-28 09:36:01.604 Offitel[6532:907] SUCCESSSS!
2012-12-28 09:36:01.605 Offitel[6532:907] status 200
2012-12-28 09:36:20.742 Offitel[6532:907] aanmelden pressed
2012-12-28 09:36:20.746 Offitel[6532:907] http://virtuele-receptie.******.sanmax.be/nl/webservice/company-user/****/*****/**************
2012-12-28 09:36:20.748 Offitel[6532:907] true
2012-12-28 09:36:22.184 Offitel[6532:907] SUCCESSSS!
2012-12-28 09:36:22.184 Offitel[6532:907] status 200
4

1 に答える 1

2

こうすれば

return _loginstatus;

操作が完了するのを待っていないため、サーバーがアプリケーションに送信しているものを返していません。この場合、私が通常行うことはUIActivityIndicator、ログイン アクションが起動されたときに表示し、サーバーが応答するまで何もしないことです。次に、変数に適切な値を設定します。この場合、ホーム画面に移動します。

を使用しているときAFNetworkingは、常に非同期処理を行っていることに注意してください。

編集

コード例:

- (void)loginActionWithPassword:(NSString *)pass
{
    // Add progress HUD to show feedback
    // I'm using MBProgressHUD library: https://github.com/jdg/MBProgressHUD#readme
    self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.progressHUD.labelText = @"Loading";
    self.progressHUD.mode = MBProgressHUDModeIndeterminate;
    self.progressHUD.dimBackground = YES;

    // Launch the action to the server
    [Actions loginWithEmail:[self.textEmail.text lowercaseString] andPassword:pass success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if ([Actions requestFailed:JSON]) {
            LOG(@"ERROR %@ / %@ / %@ \n", pass, request, response, JSON);
            if ([JSON[@"MSG"] isEqualToString:@"USER_NOT_REGISTERED"]) {
                self.progressHUD.labelText = @"User doesn't exist";
                self.textEmail.text = @"";
                self.textPassword.text = @"";
            } else if ([JSON[@"MSG"] isEqualToString:@"PASSWORD_INCORRECT"]) {
                self.progressHUD.labelText = @"Wrong password";
                self.textPassword.text = @"";
            }
            [self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
            return;
        }
        // If everything went OK, go to this function to save user data
        // and perform segue to home screen (or dismiss modal login view)
        [self onLoginSuccessWithPassword:pass JSON:JSON response:response];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // If something fails, display an error message
        LOG(@"ERROR:(%@): %@ / %@ / %@ / %@ \n", pass, request, response, error, JSON);
        self.progressHUD.labelText = @"Something went wrong, try again";
        [self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
        return;
    }];
}
于 2012-12-28T08:06:55.063 に答える