shouldStartLoadWithRequest is never called

user2720747 picture user2720747 · Aug 27, 2013 · Viewed 16.9k times · Source

I've researched and researched and still don't understand why shouldStartLoadWithRequest is never called. My page loads fine and some of the UIWebview delegate protocol methods are called. Please find relevant snippets from my code below:

Synthesize my webview in my .m (defined in a header file):

@implementation PortViewController

@synthesize WebView = myWebView;

I load my webview successfully:

    myURLString = [NSString stringWithFormat:@"https://%@", defaultWebsite];

    myURL = [NSURL URLWithString: myURLString];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [myWebView loadRequest:myRequest];

set my delegate to self

- (void)viewDidLoad
{


[super viewDidLoad];
[myWebView setOpaque:NO];
[myWebView setBackgroundColor:[UIColor clearColor]];

//myWebView.description.

myWebView.delegate = self;
}

all of my protocol methods are called EXCEPT shouldStartLoadWithRequest

- (void)webViewDidStartLoad:(UIWebView *)myWebView {
    [activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)myWebView {
    [activityIndicator stopAnimating];
}

- (BOOL)WebView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request          navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"inside Webview");
if([[request.URL absoluteString] hasPrefix:@"http://www.nzx"]) {
    // do stuff
    NSLog(@"Have caught the prefix");
    return YES;
}

    return NO;
}

Thanks in advance.

Answer

Mani Kandan picture Mani Kandan · Aug 27, 2013

Try setting the delegate in ViewWillAppear instead of ViewDidLoad

-(void)viewWillAppear:(BOOL)animated
{
     myWebView.delegate = self;
}

and include the webview delegate in your interface of PortViewController.m file

@interface PortViewController ()<UIWebViewDelegate>


@end

It should work.