How to stop the NetworkActivity on a UIWebView? - iphone

I load some content on my UIWebview ,and when the UIWebView load finish the content of a URL such as "http://www.google.com", but the network NetworkActivityIndicator didn't stop,any suggestion?
I found somebody use [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES],but I think this is just to hidden the NetworkActivityIndicator ,in fact it does't stop,is that right??
Now what I want is really to stop the NetworkActivityIndicator ,how to accomplish it??

In your UIWebView delegate:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
Remove any other calls to -setNetworkActivityIndicatorVisible in this delegate.'
If you haven't already assigned a delegate for this UIWebView, then do so as follows:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[webView setDelegate:self];
[self.view addSubview:webView];
This of course assumes you are inside a UIViewController and that self.view exists, so change as needed.

#McGrady:Try this..
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidfinishLoad");
[activity stopAnimating];
}
Here activity is your activityindicator name
Hope this help you..
Regards
Renuga

Related

Stop Activity Indicator in Status Bar?

I'm near completing my application, but I'm running into an issue. Obviously, my web view needs an activity indicator for the app to be accepted. I have the following already:
- (void)viewDidLoad
{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#""]]]; // I removed the website for privacy's sake
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Problem is that it won't stop spinning when I need it to (i.e. even after the page is loaded, it doesn't stop)
Any suggestions? Any help is appreciated!
Implement the UIWebViewDelegate in your class file that holds the UIWebView, and insert the following two methods:
- (void)webViewDidStartLoad:(UIWebView *)webView
- (void)webViewDidFinishLoad:(UIWebView *)webView
In the start method, place your:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
... and in the finish, place:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Hope this helps!
In the web views delegate you can do:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
you need to call
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
to get it to stop

webview in iphone

I am showing a particular website in UIWebView were it shows the entire website.
The problem is that I need to scroll lengthwise and breathwise to see the content, what I want is the entire website to fit the screen with the letters minimized and not to scroll lengthwise or breadthwise.
below is the code I use:
-(void)viewDidLoad
{
[super viewDidLoad];
self.title=#"MainSite";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.urlAddress4 = #"http://www.livingwaterscf.org/";
self.url4 = [NSURL URLWithString:urlAddress4];
self.requestObj4 = [NSURLRequest requestWithURL:url4];
NSURLConnection *connection=[[[NSURLConnection alloc] initWithRequest:self.requestObj4 delegate:self]autorelease];
[webViewGive loadRequest:requestObj4];
//[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
Set property scalesPageToFit to YES on your web view and your done!
Like:
webViewGive.scalesPageToFit=YES;
You probably want this :
webView.scalesPageToFit = YES;

How can I dismissModalViewController when a user hits GO?

I have a view and a button. When the button is pushed, I present a modal view controller that has a uiwebview, and a navigation bar at the top. The hardcoded uiwebview shows a login prompt. Once the user goes in and enters the correct login credentials and hits GO on the keyboard, I not only need it to authenticate me into the server, but also dismiss the modal view controller.
Note the text field I enter data into is part of the website loaded in uiwebview...
When I hit enter now, the web page authenticates, and everything is peachy, but I just need a way to tie in a
[self dismissModalViewControllerAnimated:YES];
command when they hit GO...any thoughts?
Thanks
EDIT
Here is the code that I am implementing in the loginView:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString: #"http://website"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
[webView loadRequest: request];
[request release];
[url release];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[self dismissModalViewControllerAnimated:YES];
return YES;
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
First determine the URL the webpage takes you to on successful login, let's say it's http://website/logedin. Implement UIWebViewDelegate, and detect when a request is made to that address:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] isEqualToString:#"http://website/logedin"]) {
[self dismissModalViewControllerAnimated:YES];
return NO;
}
return YES;
}
you can try with below sample code;
#interface WebViewController : UIViewController {
}
UIWebViewDelegate having few delegate methods, you have to implement it in WebViewController.m (for eg).
-(void) webViewDidStartLoad:(UIWebView *)webView{
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
-(void) webViewDidFinishLoad:(UIWebView *)webView
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}
Before this, you have to set the delegate for UIWebView from Interface Builder.

How do you start activity indicator?

I want to start an activity indicator whenever the user presses any link on a webview.
First assign the ViewController which the UIWebView appears in as its delegate: Do this either by control-dragging from the UIWebView to the File's Owner object in InterfaceBuilder or in code:
[myWebView setDelegate:myViewController];
Then in your ViewController.m file you can now use the delegate methods to detect when pages are being loaded. These delegate methods will be triggered every time a link or new page is loaded in the UIWebView.
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
If you wanna show network activity indicator just put
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
and then
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
when you wanna hide it
while if you needs UIActivityIndicator
you have to define it in your controller then Outlet it in XIB and then
show
[activityIndicator startAnimating];
hide
[activityIndicator stopAnimating];
Take a look at UIWebViewDelegate Protocol.
It will allow your delegate to be notified whenever the webview is starting to load new content, which, presumably, is the case whenever a link has been pressed. The delegate method you want is webViewDidStartLoad: - in its implementation you could then easily set up or start your activity indicator.
Use the method webViewDidStartLoad: in UIWebViewDelegate.

Throwing webview request to another webview

I have a UIViewController with a UIWebView (webview1) in it. The webview is only a few lines of text but does have a link in it. Rather than open the link, which goes to an external website, in this tiny space, I'd like to send it on to webview2, which will be a full screen. The goal is to keep the web requests within my app rather than Safari. Instead of creating another controller for webview2, I'd like to use webview1's controller.
In Webview1Controller controller, I do this in webViewLoad:
webview1.delegate = self;
Here's where I hand off the web request to webview2, which works fine:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
myapp *delegate = [[UIApplication sharedApplication] delegate];
Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:#"webview2" bundle:nil];
//self.view = webview2;'
[delegate.navigationController pushViewController: webview1Controller animated:YES];
[webview1Controller.webview2 loadRequest:request];
[webview1Controller release];
return YES;
}
In Interface Builder, I have webview2.xib File's Owner class set to Webview1Controller. It's "view" and the webview2outlet are connected. I have an IBOutlet in Webview1Controller named webview2outlet.
When i go back to webview1, it has also loaded the same request. Is there a way to stop webview1 from loading anything? If I return NO in the above method, webview1 won't render my content.
One solution is just to reload webview1 content on viewWillAppear, which works. But is there a better way?
Return NO from the delegate method.
Regarding your comment, I think what you want to do is make your delegate method check which webview is calling your controller:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (webView == webview1) {
Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:#"webview2" bundle:nil];
[self.navigationController pushViewController:webview1Controller animated:YES];
[webview1Controller.webview2 loadRequest:request];
[webview1Controller release];
return NO;
}
else {
return YES;
}
}
(Note, also, that UIViewController has a navigationController property so you can use that rather than getting it through your app delegate).
What happens if you load a copy of the request instead of the original, and then return NO?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
myapp *delegate = [[UIApplication sharedApplication] delegate];
Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:#"webview2" bundle:nil];
//self.view = webview2;'
[delegate.navigationController pushViewController: webview1Controller animated:YES];
[webview1Controller.webview2 loadRequest:[[request copy] autorelease]];
[webview1Controller release];
return NO;
}