Stop Activity Indicator in Status Bar? - iphone

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

Related

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;

UIwebview loader on iphone

this code works fine , its show activityIndicator loading and hide after loading
but lets say if its on loading stage and then i go back then my apps quit , so how to avoid this
if i go back quickly withiut site fully loaded then it quits ( i think activityIndicator is culprit)
**
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
// myLabel.hidden = FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
activityIndicator.hidesWhenStopped=TRUE;
//myLabel.hidden = TRUE;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.title=#"KKH Website";
[super viewDidLoad];
NSURL *requestUrl = [NSURL URLWithString:#"http://www./index.html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
[aWw loadRequest:requestObj];
}
**
updates
if i link uiwebview delegate to fileowner then its creating isuee , but i need delegate also since i want to show loading
i forgot to release my webview :(
[aWw release];
now its fixed

How to stop the NetworkActivity on a UIWebView?

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

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.

networkActivityIndicatorVisible

Is this code correct to use with the networkActivityIndicatorVisible?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIApplication* app2 = [UIApplication sharedApplication];
app2.networkActivityIndicatorVisible = YES;
[self loadSources]; // Loads data in table view
app2.networkActivityIndicatorVisible = NO;
}
Teo
Since NetworkActivityIndicatorVisible can be set from several points while a connection is still active, you need to track the number of calls that enable/disable it. The following UIApplication category does just that using a static variable:
// file UIApplication+NetworkActivity.h
#interface UIApplication (NetworkActivity)
- (void)showNetworkActivityIndicator;
- (void)hideNetworkActivityIndicator;
#end
// file UIApplication+NetworkActivity.m
#import "UIApplication+NetworkActivity.h"
static NSInteger activityCount = 0;
#implementation UIApplication (NetworkActivity)
- (void)showNetworkActivityIndicator {
if ([[UIApplication sharedApplication] isStatusBarHidden]) return;
#synchronized ([UIApplication sharedApplication]) {
if (activityCount == 0) {
[self setNetworkActivityIndicatorVisible:YES];
}
activityCount++;
}
}
- (void)hideNetworkActivityIndicator {
if ([[UIApplication sharedApplication] isStatusBarHidden]) return;
#synchronized ([UIApplication sharedApplication]) {
activityCount--;
if (activityCount <= 0) {
[self setNetworkActivityIndicatorVisible:NO];
activityCount=0;
}
}
}
#end
Now import UIApplication+NetworkActivity.h in your client code and call
// on connection started:
[[UIApplication sharedApplication] showNetworkActivityIndicator];
// on connection finished:
[[UIApplication sharedApplication] hideNetworkActivityIndicator];
If your concern is that the indicator blinks for only a second, you don't need a background process. Just call [self performSelector:#selector(loadSources) withObject:Nil afterDelay:0.1] so the UI thread has time to start the network indicator animation before you block the main thread.
If you're not using AFNetworking (https://github.com/AFNetworking/AFNetworking) already you can check out their network activity indicator implementation in AFNetworkingActivityIndicatorManager.
If you do choose to use this library for your network access, they handle the network activity indicator for you automatically. All you need to do is make one call in your AppDelegate to set it up, they do the rest of the work for you.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
}
An shorter solution for tracking multiple activities - again using a UIApplication category and a static variable:
#interface UIApplication (NetworkActivityIndicator)
- (void)toggleNetworkActivityIndicatorVisible:(BOOL)visible;
#end
#implementation UIApplication (NetworkActivityIndicator)
-(void)toggleNetworkActivityIndicatorVisible:(BOOL)visible {
static int activityCount = 0;
#synchronized (self) {
visible ? activityCount++ : activityCount--;
self.networkActivityIndicatorVisible = activityCount > 0;
}
}
#end
I finally solved it. I used performSelectorInBackground to execute the load data into tableView
-(void)beginLoadSources {
[self loadSources]; // Loads data in table view
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self performSelectorInBackground:#selector(beginLoadSources) withObject:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
Use thins code in your ExempleUIWebView.m
before - (void)viewDidLoad
(void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[ProgressHUD dismiss];
}
and use this after - (void)viewDidLoad
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[ProgressHUD show:#"Loading Privacy Policy" Interaction:NO];