Search code examples
iosobjective-cobjective-c-category

Common webViewDidStartLoad by category


I think implement completely same method to every viewController with UIWebView (and delegate=self)is not smart. So tried to setup common loading method with all UIWebView. But it did not work. Is it wrong to achieve with category?

UIWebView+Loading.m

-(void)webViewDidStartLoad:(UIWebView*)webView
{ 
   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

ViewController.h

@interface ViewController : UIViewController <UIWebViewDelegate>

ViewController.m

#import "UIWebView+Loading.h"

//abbr...

-(void)viewWillAppear:(BOOL)animated
{
    UIWebView *someWebView = [[UIWebView alloc] init];
    someWebView.delegate = self;

    //and HTTP request
    NSURLRequest *req = (abbr);
    [someWebView loadRequest:req];
}

Solution

  • The method webViewDidStartLoad: is not being called because it is a part of UIWebViewDelegate protocol, not a method of UIWebView class itself. You have to implement it in your ViewController.m file.

    Also, you may want to read about delegation pattern in iOS.