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
OKE謝謝。但是,你可以給一些代碼示例嗎? – Steaphann
我已經添加了一些示例代碼。 – amb
Oké謝謝,但你能解釋一下[Utils hudDuration來自哪裏? – Steaphann