Kamal Upasena
Kamal Upasena

Reputation: 1379

add ui progress view ios

I use the following code to download a JSON from a web service.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"my web service "]];

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

I create my user interface according to this JSON string and I need to add a progress bar to my code. I have tried many examples but I cannot get the progress bar to display properly. Can anybody tell me how to use a progress bar to till my view did load. It will be a great help, thank you.

Upvotes: 0

Views: 1201

Answers (1)

alexgophermix
alexgophermix

Reputation: 4279

If you haven't decided on what kind of progress dialog to use yet, I recommend MBProgressHUD:

https://github.com/jdg/MBProgressHUD

It's fairly easy to use, and seems to be what you would need for this case. If you're set on making the request synchronous, at the desired point in your view lifecycle e.g. viewDidLoad: you could have something like the following:

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

// synchronously pull down the necessary JSON
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"my web service "]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// perform the necessary view configuration you need to do based on this data
// .
// .
// .

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

This will display a blocking spinner while your configuration is occurring and then clear it when the process is complete.

Upvotes: 1

Related Questions