views:

510

answers:

1

Hi,

I'm loading a remote URL in a Webview, and I want to show a spinner while the content is loading. I have the following code, but the spinner doesn't go away when the content finishes loading. How do I make it so the spinner disappears after the content is loaded?

NSURL *noteURL = [NSURL URLWithString:@"http://google.com/"];
NSString *defaultNote = @"Hello there";

NSRect frame = [[noteView superview] frame];
noteSpinner = [[[NSProgressIndicator alloc] initWithFrame:NSMakeRect(NSMidX(frame)-16, NSMidY(frame)-16, 32, 32)] autorelease];
[noteSpinner setStyle:NSProgressIndicatorSpinningStyle];
[noteSpinner startAnimation:self];
//webViewFinishedLoading = NO;
[[noteView superview] addSubview:noteSpinner];

if (noteURL)
{
    if ([noteURL isFileURL])
    {
        [[noteView mainFrame] loadHTMLString:@"Release notes with file:// URLs are not supported for security reasons—Javascript would be able to read files on your file system." baseURL:nil];
    }
    else
    {
        [[noteView mainFrame] loadRequest:[NSURLRequest requestWithURL:noteURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]];
    }
}
else
{
    [[noteView mainFrame] loadHTMLString:defaultNote baseURL:nil];
}
+1  A: 

Be the frame load delegate, and stop the progress indicator's animation when the main frame finishes loading.

Only stop the animation when the frame that you received a did-finish-load message for is the web view's main frame. You'll get one of those messages for every frame in the web view, including nested frames and iframes. You don't want to stop the animation prematurely.

Peter Hosey
How do I know when the main frame finishes loading?
Chetan
What I mean is, where do I put the `stopAnimation` command?
Chetan
Never mind, got it. For the record, you need to define the following function in the same class: - (void)webView:(WebView *)sender didFinishLoadForFrame:frame { if ([frame parentFrame] == nil) { [noteSpinner setHidden:YES]; [sender display]; } }
Chetan
The other way to test for the main frame would be to ask the web view for its main frame and check whether the frame that finished loading is the same frame.
Peter Hosey