2013-11-22 53 views
1

我想在調用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]; 

    } 
} 
} 
+0

您最好在後臺調度您的請求.. – amar

+0

您必須決定您的視圖控制器在數據被提取之前是否無法使用,您可以通過使用MBProgresshud或以自己的方式顯示任何活動指示器......但如果您的視圖控制器在不提取數據的情況下可用,則在後臺獲取數據。 – amar

+0

@amar:請在後臺詳細說明調度請求。 – Mak13

回答

1

大部分的答案是部分不中你的代碼指出的主要問題。

主線程上的同步請求會阻止它,因此當同步請求正在進行時,您不能顯示任何UI更新(如活動指示符)。而是使用異步請求(或使用GCD在後臺發出請求)並使用UIActivityIndicatorView或任何其他可用於此的開放源代碼。

按照這個問答& A到學習如何使ASYN要求使用GCD:NSURLConnection and grand central dispatch

您可以創建並添加一個活動指標的視圖。當請求啓動時呈現並開始顯示活動,並在請求完成下載時停止活動。

希望有幫助!

+0

感謝指南@amar,我會這樣做。 – Mak13

0

https://github.com/jdg/MBProgressHUD

下載爲MBProgressHUD項目,並複製MBProgressHUD.h & .m類到您的項目,現在通過簡單創建MBProgressHUD的實例,並使用可用的方法,通過MBProgressHUD您就可以在加載視圖到你的Web服務

HUD = [[MBProgressHUD alloc] initWithWindow:[[UIApplication sharedApplication] delegate].window]; 
[HUD showWhileExecuting:@selector(loginUserintoserver) onTarget:self withObject:nil animated:YES]; 
[[[UIApplication sharedApplication] delegate].window addSubview:HUD]; 

希望它會幫助你。

3

你必須用這個:MBProgressHUD

導入framwork從上面的鏈接,並做一些東西,如:

.h文件中:

#import "MBProgressHUD.h" 

    MBProgressHUD *HUD; 

.m文件做到這一點:

 HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
     [self.view addSubview:HUD]; 

     [HUD show:YES]; 
     // call your webservice here 

     [HUD hide:YES]; 

可能會有所幫助。

快樂編碼... :)

+0

你的ans和@Erhan之間有什麼不同? –

+0

@NitinGohel我在他的回答之前。 –

2

麥,

我用MBProgressHUD我所有的項目。您也可以使用.Real MBProgressHUD是合適的,輕量級的。

顯示指標:

[MBProgressHUD showHUDAddedTo:self.view animated:YES]; 

隱藏:

[MBProgressHUD hideHUDForView:self.view animated:YES]; 
相關問題