我想在調用loginUserintoserver方法時顯示加載視圖控制器或活動指示器視圖,但在調試時發現視圖在此行變爲非活動狀態。如何在我的webservices中顯示加載視圖控制器
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
有一段時間,直到我得到迴應。我試圖顯示活動指標,但沒有成功。請任何準則來解決這個問題。提前致謝。
-(void) loginUserintoserver
{
NSString *str_validateURL = @"callogin";
// EM,密碼,devicereg,設備類型,標記=( 「E」 或 「M」)
NSString *str_completeURL = [NSString stringWithFormat:@"%@%@", str_global_domain, str_validateURL];
NSURL *url = [NSURL URLWithString:str_completeURL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
str_global_UserPhone = [NSString stringWithFormat:@"%@%@",signupCountryCodeTextField.text,signupMobileTextField.text];
NSString *postData = [NSString stringWithFormat:@"em=%@&password=%@&devicereg=%@&devicetype=%@&flag=%@", loginEmailTextField.text, loginPasswordTextField.text, str_global_DeviceRegID, @"1", [NSString stringWithFormat:@"%@", emailphoneFlag]];
NSLog(@"==============%@",postData);
NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
// here the view becomes inactive and takes time to get response from server
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSLog(@"response data is %@", responseData);
if (responseData == nil)
{
NSLog(@"No data from server");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"No data downloaded from server!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
else
{
NSLog(@"response data is %@", responseData);
NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"returnString....%@",returnString);
NSDictionary *response_dic = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSString *msg;
msg = [response_dic objectForKey:@"Result"];
NSDictionary *loginDict=[[NSDictionary alloc]init];
loginDict=[response_dic objectForKey:@"Result"];
NSLog(@"msg is : %@ ",[response_dic objectForKey:@"Result"]);
if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"0"])
{
// success
NSLog(@"Successfull Login!!!!!");
NSString *UserId=[loginDict objectForKey:@"userid"];
[[NSUserDefaults standardUserDefaults] setValue:UserId forKey:@"LoginId"];
[self initRevealViewController];
} else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"1"]){
NSLog(@"Invalid Password!");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"ReEnter Password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"3"]){
NSLog(@"Invalid input parameters!");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Email address or Mobile number, Password, devicereg, devicetype, flag are Mandatory" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}else if ([[[response_dic objectForKey:@"Result"] objectForKey:@"ErrorCode"] isEqualToString:@"6"]){
NSLog(@"Invalid input parameters!");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"User Registered but not activated" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
}
您最好在後臺調度您的請求.. – amar
您必須決定您的視圖控制器在數據被提取之前是否無法使用,您可以通過使用MBProgresshud或以自己的方式顯示任何活動指示器......但如果您的視圖控制器在不提取數據的情況下可用,則在後臺獲取數據。 – amar
@amar:請在後臺詳細說明調度請求。 – Mak13