Stop network activity indicator

I used the code below to load a web page. Everything is working fine, but I want to stop the network activity indicator after the completion of loading the web page. How can we know that the webpage is fully loaded.

Someone please help.

UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; 
+4
source share
4 answers

The easiest way to do this is to add this line after creating your UIWebView.

 [webView setDelegate:self]; 

Now you will call webViewDidFinishLoad: and the whole method should look like this.

 - (void)webViewDidFinishLoad:(UIWebView *)webView { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } 

I will explain this further, because in the future you will need to understand the delegation.

UIWebView is just an object that displays web data. Actually, I don’t really like it when I need to process all this, because he really just loves web data. Now, in your case, you wanted to know when the UIWebView was done, doing your favorite little task. To do this, your UIWebView gives him a scream like "Yo Holmes, now I load this data, so do whatever you do now." and your UIWebView continues to have fun.

So what we did, we told UIWebView that its delegate, in this case, was our current class by setting the delgate property in the webView for ourselves.

From there, you can call any of the methods available to the UIWebView delegate (all of which are in the documentation) and force them to perform tasks secondary to the main purpose of the UIWebView.

Make sense?

+6
source

You just need to set webView.delegate to point to some object and implement this webViewDidFinishLoad: object.

+3
source

Here is what I used to stop the activity indicator:

 - (void)viewDidLoad { [super viewDidLoad]; NSString *urlAddress = [NSString stringWithFormat:@"http://www.wikipedia.com/wiki/%@", place.name]; NSURL *url= [NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; [[self webView] setDelegate:self]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [activityIndicator stopAnimating]; } - (void) webViewDidStartLoad:(UIWebView *)webView { [activityIndicator startAnimating]; } 
0
source

To show the network indicator while the request to load the UIWebView is used below the code.

 #pragma mark - UIWebViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { return YES; } //For Showing NetWork Indicator in Webview - (void)webViewDidStartLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } 

Note: UIViewController<UIWebViewDelegate> and [webView setDelegate:self];

0
source

Source: https://habr.com/ru/post/1306427/


All Articles