2012-12-28 21 views
1

我正在製作一個使用AFNetworking庫的項目。我用它來檢查web服務的登錄名和密碼。如果沒問題,這給了我一個200碼的代碼。如果它是200,我將布爾值設置爲true,以便應用程序可以繼續。但是這個布爾值沒有在正確的時間設置。在它工作之前,我總是需要在我的login button上按兩次。這是我的代碼來設置布爾值。AFnetworking waitUntilFinish不起作用

- (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],應用程序崩潰。

有人可以幫助我嗎?

親切的問候

LOG 這裏是我的日誌的打印後,我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 

回答

2

如果你這樣做

return _loginstatus; 

你並沒有等待手術完成,所以你沒有返回你的手術rver正在發送到您的應用程序。我通常在這種情況下做的是在登錄操作被觸發時顯示一個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; 
    }]; 
} 
+0

OKE謝謝。但是,你可以給一些代碼示例嗎? – Steaphann

+0

我已經添加了一些示例代碼。 – amb

+0

Oké謝謝,但你能解釋一下[Utils hudDuration來自哪裏? – Steaphann