Unable to see UIActivityIndicator in UIWebView - iphone

I am new to iPhone developer,
I am loading pages in my webview, loading takes too much time so i wan to show ActivityIndicator till the page loads,
Here is my code snippet but it is not working,
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
[_webview bringSubviewToFront:activityIndicator];
...
- (void)webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
activityIndicator.hidden=FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
activityIndicator.hidden=TRUE;
}
but i am unable to see my activityIndicator
Any help will be appreciated.

You are adding the UIActivityIndicatorView directly to the self.view and later you are trying to bringSubviewToFront from the UIWebView. Check with:
[self.view bringSubviewToFront:activityIndicator];

Change [_webview bringSubviewToFront:activityIndicator]; to [self.view bringSubviewToFront:activityIndicator];

You can also change the order in which you add subviews. Add the webview first, and then add the activity indicator:
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);//you should also keep the height and width equal
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
And get rid of the line that follows:[_webview bringSubviewToFront:activityIndicator];

Related

Show and Hide ActivityIndicator using SDWebImageDownloader

I am loading image using SDWebImageDownloader classes on every button click in my view.
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = mainImage.center;
activityIndicator.hidesWhenStopped = YES;
[activityIndicator startAnimating];
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholderLarge.png"] options:0 andResize:CGSizeMake(mainImage1.frame.size.width,imgview.frame.size.height) withContentMode:UIViewContentModeScaleAspectFit];
[imgView addSubview:activityIndicator];
I am not getting where to remove activityIndicator
You can use the following block.. Its more effective and it works for me...
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(UIImage *image, BOOL cached){
[activityIndicator stopAnimating];
} failure:^(NSError *error){
//do any additional tasks if its failure
}];

How can I add activityIndicator during viewload while fetching data from web services?

How I add an UIActivityIndicatorView before the viewload. I am fetching data from a web server.
Here is my code
spinner=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
[spinner setBackgroundColor:[UIColor clearColor]];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:spinner];
[spinner startAnimating];
objCountryPreferences=[[CountryPreferences alloc]init];
objLanguagePreference=[[LanguagePreference alloc]init];
objMobilePrefixParser=[[MobilePrefixParser alloc]init];
//this is calling different web services
[objMobilePrefixParser getMobilePrefix];
[objLanguagePreference languagePreference];
[objCountryPreferences getCountryIdArr];
Add ActivityIndicator in separate method and call it using
[self performSelector:#selector(addActivityIndicator) withObject:nil afterDelay:0.1];
Do you mean the progress indicator is not animating. If YES, you need to perform startAnimating in separate thread.
[spinner performSelector:#selector(startAnimating)]
Create spinner :
self.spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.spinner.frame=CGRectMake(152,183,60,60);
// display spinner in StatusBar it Optional for you
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.spinner sizeToFit ];
/*-----------Change Size of UIActivityIndicatorView-------------*/
self.spinner.transform = CGAffineTransformMakeScale(2, 2);
[self.webView addSubview:self.spinner];
Use Delegate method of UIWebView:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[self.spinner startAnimating];
// display spinner in StatusBar it Optional for you
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.spinner stopAnimating];
// Remove spinner in StatusBar it Optional for you
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

UIActivityIndicator in xcode?

I m trying out for the concept of UIActivityIndicator.In the firstView I have a tableView loaded with data and corresponding accessorybutttons.So when a accessorybutton of a tableViewCell is tapped then DetailsView is loaded.Meanwhile Im adding an activityIndicator when accessorybutton is tapped.
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
DetailsView *detailView= [[DetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UILabel *empid=(UILabel*)[cell viewWithTag:117];
detailView.Id=[empid text];
[self presentModalViewController: detailView animated:NO];
[detailView release];
}
Then in DetailsView based on the empid it received from firstView it loads some data
- (void)viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
////loads data from service url and parsing is done
[activityIndicator stopAnimating];
}
Until here it is working perfectly..I have a back button in DetailsView and whenever it is pressed presentmodalViewController is dismissed.
-(IBAction)btnBack
{
[self dismissModalViewControllerAnimated:NO];
}
But now the problem is I can see the presentmodalViewController getting dismissed but the activityindicator which started animating when accessorybutton is tapped still animating.So how do I stop that animating activityIndicator when presentView is dismissed and display only the data of prevoius view.
in ViewWillAppear method of first view write below code
[activityIndicator hidesWhenStopped];
tell me whether it is working or not!!!
Happy coding!!!!
Use following code
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
detailView.Id=[empid text];
[activityIndicator stopAnimating];
[self presentModalViewController: detailView animated:NO];
}
or in your viewWillDisappear of FirstView
{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperView];
}
in DetailsView class in viewDidLoad method just add activityIndicator as a subview and start animating like bellow..
- (void)viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityIndicator.color = [UIColor blackColor];
[self.view addSubview:activityIndicator];
activityIndicator.frame=CGRectMake(140, 195, 37, 37);
[activityIndicator startAnimating];
}
And also stopAnimating when your data download complete or fail and also in bellow method add in btnBack method or viewWillDisappear just add this bellow code
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];///Add this line if you want to remove from superview
In the viewWillAppear of first view add [activityIndicator stopAnimating];
-(void)viewWillAppear:(BOOL)animated
{
if (activityIndicator)
{
[activityIndicator stopAnimating];
}
}

get UIWebView to load in CGRect correctly

I'm trying to get my webView to load correctly in the CGRect frame I set up, with the activityIndicator running.
It kind of loads correctly when I have it set up with [self.view addSubview:webView]; but the activityindicator doesnt appear. When I set it up with webView.delegate = self;the activityindicator does appear, but the webview covers up the whole screen when it loads up. I have a segmentedbar up top that gets covered up then.
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webFrame = CGRectMake(0.0, 50.0, 320.0, 318.0);
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(250, 250, 30.0, 30.0);
self.view.center = CGPointMake(160.0f, 190.0f);
activityIndicator.center = self.view.center;
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
// [self.view addSubview:webView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"activity started");
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
self.view = webView;
}
It should be
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
[[self view] addSubView:webView];
}
The views weren't being covered, they were being removed from the hierarchy by resetting the view property.

Implementing activity indicator

I am implementing an activity indicator in my application. In my application on button click a webservice is called and it takes some time. To show the user that process is going on I implemented an activity indicator:
CGRect frame = CGRectMake(140, 300, 40, 37);
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:frame];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
This Snippet is written in viewDidLoad() method, and I have an action called,
-(IBAction)agree:(id)sender
{
//here webservice is called
}
I have to start that activity by [activityIndicator startAnimating];
But I am unable to start that activityIndicator, please suggest a proper solution for that.
Put this code into your web view.
-(void) viewDidLoad
{
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);
activityIndicator.center=self.view.center;
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
Refer this link link text. This explains to display the activity indicator for parsing action of the rss feed
Best of Luck.