How to properly use the webkit delegate to start and stop a spinner, OS X, OBJ-C
-
12-12-2019 - |
Question
It seems like this should be so simple, but I am pretty new at Objective-C. What I want to do is simply start and stop a spinner while my WebView is loading. This is an OS X app. Everything I have searched for is for Cocoa Touch, I am using just Cocoa. In my AppDelegate.m I have to methods that start and stop the spinner (This does work, I tested it).
-(IBAction)goSpin:(id)sender
{
[spinner startAnimation:self];
}
-(IBAction)stopSpin:(id)sender
{
[spinner stopAnimation:self];
}
I also have the two delegate methods for webView, which I overrode.
-(void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
[self goSpin:self];
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[self stopSpin:self];
}
Basically, I would like to know how I get my webView to set it's delegate. Usually I have to do something in the .h file, but I can't find any references that list what the webKit delegate is that would work for this. Any help would be appreciated.
Solution
The
-(void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
and
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
methods are part of the WebFrameLoadDelegate Protocol.
WebView has a frameLoadDelegate
property. Set it on your WebView
instance by calling [webView setFrameLoadDelegate:delegate]
, where delegate
is an NSObject that implements the two methods (it will be easiest for you to make self
the frameLoadDelegate here). Since WebFrameLoadDelegate is an informal protocol, delegate
should declare the two methods in its .h file, rather than add <WebFrameLoadDelegate>
to its class declaration as with a formal protocol.
OTHER TIPS
You can override these webview delegate methods to start and stop a spinner.
- (void)webViewDidStartLoad:(UIWebView *)webView{
// In this method you have to start a spinner.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// In this method you have to stop the current spinner.
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
// In this method you have to stop the current spinner.
}