views:

337

answers:

2

I have an app that works with tabs and webview. I already have it setup to refresh the page assigned to a tab each time the item on tabbar is selected. My problem now is that it takes some time to load the page and it's impossible to say if the page being displayed is the old or refreshed one.

What I want to do is add a "loading" screen (a simple image) which will be displayed until the refreshed page is loaded. Here is the function I run on each tab tap:

- (void) goToPage:(NSString *)sid
{
    NSString *newURL = [NSString stringWithFormat:@"%@/mykingdom.php?sid=%@", appURL, sid];

    [secondView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newURL]]];
}

Thanks!

A: 

The preferred UI is to show a large UIActivityIndicatorView. That is the "gear" that rotates a highlight as if turning.

If you wanted something else, you could just display a modal view over the UIWebView with some graphic in it and then dismiss it when the web view reports itself done loading.

If you wanted a web view to display something, the easiest way would be to have to web views one showing the loading page and the other loading the actual page. Swap them out as needed. That gets around having to manage the downloading to a string then loading into a web view which can sometimes get messy for complex web sights.

TechZen
UIActivityIndicatorView is not something I'm looking for, because I want to hide the "old" page to avoid user confusion. What do you mean by "modal view over the UIWebView" and how would a code for that look like?
AragornSG
+1  A: 

UIWebViewDelegate has 2 handy methods:

– webViewDidStartLoad:(UIWebView *)webView
– webViewDidFinishLoad:(UIWebView *)webView

you can easy implement and do whatever you like. Cheers,

marcio
Thanks Marcio!Unfortunately I need a bit more help. Where do I put this code? I tried adding it to the class assigned to appropriate webview, but it doesn't get launched when I load a page.
AragornSG
I have no idea about your code. Anyway I suppose you are using two instances of uiwebview (one is your secondView) maybe inside a viewcontroller. The viewcontroller is a good place where you can implement the two delegate methods, remember to set the delegate property of each webview to the right object (firstView.delegate = self, secondView.delegate = self): maybe you've forgotted this two lines of code. Otherwise you can extend webview and manage its delegate methods directly inside of the webview class: if you don't need any particular other object reference this can be a very chic way.
marcio
ok, I figured it out, added Image view and toggle it hidden or visible in those two functions.
AragornSG