Stopping network activity indicator - iphone

I used the below code to load a webpage. Everything works fine, but I want to stop the
network activity indicator after completing loading the webpage. How can we know that
the webpage is loaded completely.
Anyone 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];

The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidFinishLoad: and the entire method should look like this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I'll explain this further since you will absolutely need to understand delegation in the future.
UIWebView is merely an object that displays web data. It doesn't really like handling all this other stuff, because it really just loves web data. Now in your case you wanted to find out when UIWebView was done doing its favorite little task. To do this, your UIWebView gives a shoutout to it's delegate like "Yo holmes, I'm done loading this data now. So go do whatever it is you do now." and your UIWebView continues on its merry way.
So what we did is we told the UIWebView that its delegate, in this case, was our current class by setting the delgate property on webView to self.
From there you can call any of the methods available to the UIWebView delegate (its all in the documentation) and have them perform tasks secondary to the main purpose of the UIWebView.
Make sense?

You just need to set webView.delegate to point to some object, and have that object implement webViewDidFinishLoad:.

To show network Indicator while UIWebView load request use below 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];

here's 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];
}

Related

shouldStartLoadWithRequest delegate method never called

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
shouldStartLoadWithRequest should do the trick, but that method is never called when I click to a link. I cannot find why this doesn't work.
I have read somewhere that I need to add this line :
webView.delegate = (id)self;
I tried it, and still get the same issue. Please help
AppDelegate.m
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString] ;
NSLog(#"Url: %#", currentURL);
return YES;
}
Controller.m
- (void)viewDidLoad
{
webView.delegate = (id)self;
NSString *tokenString = #"123";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"http://192.168.1.69:8888/webapp/test.php?uid=%#", tokenString];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
If your Controller instance adds itself as the delegate then the method you add to the AppDelegate instance will never be called.
Move the shouldStartLoadWithRequest method into your Controller class.
You've set your view controller as the web view's delegate. But then you've implemented the UIWebView delegate method in our application's delegate instead.
Try implementing that delegate method in your view controller. Your view controller is your web view's delegate.

How to show loading image while loading the content of the web view in iPhone application

I have an url which need to access in web view and the url is accessing successfully. but the issue is time is taking more to load the url in webview and hence I need to show a loading image while loading an url. I am trying for that, but not able to get it to show loading bar, loading icon or whatever it may be.
And my code is here goes
NSLog(#"Response ==> %#" ,encodedString);
//'encodedstring' is my url which need to access
// code to show a loading image
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 320, 470);
[self.view addSubview:indicator];
[indicator release];
[indicator startAnimating];
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
[webView setDelegate:self];
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
url2 = [[NSURL alloc] initWithString:urlTwo];
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
[[self view] addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView stopLoading];
}
Can anybody is here to solve my issue and make the day happy. Thanks in advance.
-(void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[indicator stopAnimating];
}
-(void)webViewDidStartLoad:(UIWebView *)aWebView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[activityView stopAnimating];
}
have u tried that?

UIWebView does not load the URL

I have very small problem while loading the URL in UIWebView.
I have one UIButton, on clicking of it, I add the UIView which contains UIWebView, UIButton & Title bar. Code used is as follows -
[self.view addSubview:vwOnline];
vwOnline.bounds = self.view.bounds;
//Load webview
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#", objWine.strOnlineURL]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[wbvwOnline loadRequest:request];
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//Show activity indicator
[indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error - %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Find A Vino" message:#"Error while loading request." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
By doing above code, most of times the UIWebView does not loads the URL which I passed in the object objWine.strOnlineURL. If I clicked the back button & again clicked on the button to load the URL, it goes into the - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error delegate method of UIWebView.
If anyone knows the solution, then please help me.
Instead of adding your webview on your button click each time. Why don't you add your webview into your viewDidLoad and hide and show it in your button click.
when you are going back, make request to nil and load empty htmlstring to webview. One more thing, remove [indicator removeFromSuperview]; line from
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
-(void)webViewDidFinishLoad:(UIWebView *)webView
use
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com"]];
if it is work fine then check objWine.strOnlineURL
otherwise it is clear :)

How to integrate Webview in iPhone?

I am working in iPhone application, Using Webview to load Addvertisement in bottom of the screen and its working fine, i want to when the user select the WebView, its automatically goes to browser and load in iPhone device, How to integrate this? please help me
Thanks in Advance
I tried this:
- (void)viewDidLoad
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
[webView setDelegate:self];
NSString *urlAddress = #"http://www.dasfafa./myadds.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *currentURL = self.AddvertiseWebView.request.URL.absoluteString;
NSLog(#"currentURL:%#",currentURL);
}
Here code showing you how to open a link in Safari when clicked on in your UIWebView. The method shouldStartLoadWithRequest is a delegate method that is called when a link is clicked on. You can override the method and tell it to open in Safari.
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL = [ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
if ( ( [ [ requestURL scheme ] isEqualToString: #"http" ]
|| [ [ requestURL scheme ] isEqualToString: #"https" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open
// in UIWebView. You could also check for the other following
// protocols: tel, mailto and sms
return YES;
}
or Try this
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES; }

Changing tel: property working in simulator but Not in Device

I have changed tel property to do some string manipulation instead of calling that number.
its working in simulator but when i do it device, its still trying to call.
i have tried with webview, textview, button still same problem :(
Any idea?
Let me rephrase
By default, when clicked on any link with tel:xxxx iPhone calls number xxxx
But i don't want calling, instead i want to grab xxxx and append that value to my String.
How to do that.
Thanks
- (void)viewDidLoad {
[super viewDidLoad];
self.htmlString = [self createHTML];
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
[webView loadHTMLString:self.htmlString baseURL:nil];
}
- (NSMutableString *) createHTML {
NSMutableString *tempString =[NSMutableString stringWithFormat:#"<div>"];
[tempString appendString:#"John Smith"];
[tempString appendFormat:#"<br> <a href='tel:201207415'>201207415</a>"];
[tempString appendString:#"</div>"];
return tempString;
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
NSString *mytext = [url absoluteString];
NSLog(#"%#",mytext);
myLabel.text = mytext;
return YES;
}
If you do not want to call:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
return NO;
}
Simulator cannot call anyway.