2014-01-06 30 views
0

我需要顯示一個UIActivityIndi​​cator,而我正在等待Web服務的響應。我到底在哪裏把它的代碼?它不以這種方式工作。活動指示器不顯示。調用webservice時顯示UIActivityIndi​​cator

我是否需要使用異步請求才能顯示它?

-(void)callWebService 
    { 
     [self.customercareSearchbar resignFirstResponder]; 
     [self.SRResultDictionary removeAllObjects]; 
     NSLog(@"web service called"); 
     NSString *srn = _SRNumber; 
     NSString *serviceURL = [NSString stringWithFormat:@"https://abcdef...]; 

     @try { 
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
       [self.view addSubview:activity]; 

       activity.center = self.view.center; 
       [self.view bringSubviewToFront:loadView]; 
       activity.hidesWhenStopped = YES; 
       [activity setHidden:NO]; 

       [activity startAnimating]; 

      NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serviceURL]]; 
      NSURLResponse *serviceResponse = nil; 
      NSError *err = nil; 

      NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&serviceResponse error:&err]; 

      [activity stopAnimating]; 

      NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err]; 

      if(!parsedData) 
      { 
       NSLog(@"data not parsed"); 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
       [alert show]; 
       [self.customerCareTableView setHidden:YES]; 
      } 
      else 
      { 
       NSLog(@"parsed"); 
       NSLog(@"parsed.. the size is %lu", (unsigned long)[parsedData count]); 
       NSLog(@"%@", parsedData); 

       NSString *status = [parsedData objectForKey:@"ns:Status"]; 
       NSLog(@"the status is %@", status); 
       if([status isEqualToString:@"Success"]) 
       { 
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

        if([[prefs objectForKey:@"SwitchState"] isEqualToString:@"OFF"]) 
        { 
         //do nothing 
        } 

        else 
        { 
         [self saveNumberInDatabase:srn]; 
        } 

        NSMutableDictionary *third = [parsedData objectForKey:@"ListOfXrxLvServiceRequest"]; 
        NSLog(@"internal dict is %@", third); 

        self.SRResultDictionary = [third objectForKey:@"ServiceRequest"]; 

        [self.customerCareTableView reloadData]; 
        [self.customerCareTableView setHidden:NO]; 
       } 

       else if([status isEqualToString:@"Record Not Found"]) 
       { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Please enter a valid Service Request Number" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
        [alert show]; 
        [self.customerCareTableView setHidden:YES]; 
       } 

       else 
       { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
        [alert show]; 
        [self.customerCareTableView setHidden:YES]; 
       } 
      } 
     } 

     @catch (NSException *exception) 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NULL message:@"Problem In Network Connection. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [self.customerCareTableView setHidden:YES]; 
     } 
     @finally { 

     } 
    } 
+3

這是最好的[MBProgressHUD(https://github.com/jdg/MBProgressHUD) –

+1

請。這人想知道如何以及何時使用UIActivityIndi​​cator。你爲什麼把他引導到第三方?教他吧。笏是問題的人。笏完全正在發生,當你使用你自己的代碼? – croyneaus4u

+0

活動指標不顯示當我使用我的代碼.. – Shradha

回答

3

是的,問題是Synchronous請求。

如果可以發送ASynchronous請求然後嘗試這樣做。

[NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

// stop activity 

// write other code you want to execute 

}]; 
+0

好吧..我會試試這個並且回覆你 – Shradha

0

我發現MBProgressHUD是最好的指標,您可以使用僅僅是在方法調用的開始像

dispatch_async(dispatch_get_main_queue(), ^{ 
    if(!HUD) HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 
    [self.view addSubview:HUD]; 
    HUD.delegate = self; 
    HUD.userInteractionEnabled = NO; 
    HUD.labelText = @"Saving your Preferences..."; 
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
    [HUD show:YES]; 
}); 

,並在finally塊,你可以隱藏這個喜歡

dispatch_async(dispatch_get_main_queue(), ^{ 
     [HUD hide:YES]; 
     [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
    }); 
+0

它si也不工作..指標沒有顯示.. – Shradha

+0

你添加了MBProgress代碼文件,並做出正確的子視圖,我做了我的tableview,你可以使用你的self.view – Retro

0
//.h file 
@interface ViewController : UIViewController 
{ 
UIActivityIndicatorView *activityIndicator; 
BOOL showingActivityIndicator; 
} 
@property(nonatomic) BOOL showingActivityIndicator; 
@property(nonatomic) UIActivityIndicatorView *activityIndicator; 
@end 


//.m file 

@synthesize showingActivityIndicator,activityIndicator; 

/////在該視圖中調用此方法DODLoad

-(void)initializeClass  
{  
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    self.activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 

    self.activityIndicator.hidesWhenStopped = YES; 
    [self layoutSubviews];  
    } 

    -(void)layoutSubviews 
    { 
    CGRect activityIndicatorFrame = self.activityIndicator.frame; 
    activityIndicatorFrame.origin.x = (self.view.frame.size.width - self.activityIndicator.frame.size.width)/2; 
    activityIndicatorFrame.origin.y = (self.view.frame.size.height - self.activityIndicator.frame.size.height)/2; 
    self.activityIndicator.frame = activityIndicatorFrame; 

    [self.view addSubview:self.activityIndicator]; 
    } 


    -(void)setShowingActivityIndicator:(BOOL)showingActivityIndicators 
    { 
    if (showingActivityIndicators) { 
     [self.activityIndicator startAnimating]; 
    } else { 
     [self.activityIndicator stopAnimating]; 
    } 
    showingActivityIndicator= showingActivityIndicators; 

    } 
    -(void)dummyButtonAction // you button action to call service 
    { 
    [self setShowingActivityIndicator:YES]; 
    [self performSelector:@selector(callWebService) withObject:nil afterDelay:0.3]; 
    // [self callWebService]; 
    } 

    -(void)callWebService 
    { 
    [self.view endEditing:YES]; // this statement will make sure keyboard is resigned 
    //[self.SRResultDictionary removeAllObjects]; 
    NSLog(@"web service called"); 
    NSString *srn = _SRNumber; 
    NSString *serviceURL = [NSString stringWithFormat:@"https://abcdef...]; 

          @try { 


           NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serviceURL]]; 
           NSURLResponse *serviceResponse = nil; 
           NSError *err = nil; 

           NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&serviceResponse error:&err]; 


           NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err]; 

           if(!parsedData) 
           { 
            NSLog(@"data not parsed"); 
            [self ShowAlertViewWithTitleString:@"ERROR":@"Problem in Network. Please Try Again!"]; 

            [self.customerCareTableView setHidden:YES]; 
           } 
           else 
           { 
            NSLog(@"parsed"); 
            NSLog(@"parsed.. the size is %lu", (unsigned long)[parsedData count]); 
            NSLog(@"%@", parsedData); 

            NSString *status = [parsedData objectForKey:@"ns:Status"]; 
            NSLog(@"the status is %@", status); 
            if([status isEqualToString:@"Success"]) 
            { 
             NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

             if([[prefs objectForKey:@"SwitchState"] isEqualToString:@"OFF"]) 
             { 
              //do nothing 
             } 

             else 
             { 
              [self saveNumberInDatabase:srn]; 
             } 

             NSMutableDictionary *third = [parsedData objectForKey:@"ListOfXrxLvServiceRequest"]; 
             NSLog(@"internal dict is %@", third); 

             self.SRResultDictionary = [third objectForKey:@"ServiceRequest"]; 

             [self.customerCareTableView reloadData]; 
             [self.customerCareTableView setHidden:NO]; 
            } 

            else if([status isEqualToString:@"Record Not Found"]) 
            { 

             [self ShowAlertViewWithTitleString:@"Invalid Entry":@"Please enter a valid Service Request Number"]; 

             [self.customerCareTableView setHidden:YES]; 
            } 

            else 
            { 

             [self ShowAlertViewWithTitleString:@"ERROR":@"Problem in Network. Please Try Again!"]; 

             [self.customerCareTableView setHidden:YES]; 
            } 
           } 
          } 

          @catch (NSException *exception) 
          { 
           [self ShowAlertViewWithTitleString:@"":@"Problem In Network Connection. Please Try Again!"]; 
           [self.customerCareTableView setHidden:YES]; 
          } 
          @finally { 

          } 

           [self setShowingActivityIndicator:NO]; 
    } 
    - (void)ShowAlertViewWithTitleString :(NSString *)title :(NSString *)message 
    { 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
+0

好吧..會盡力回覆你.. – Shradha

+0

不知道爲什麼,但它仍然沒有顯示指標 – Shradha

+0

檢查代碼現在我已經更新了它 –

相關問題