user2720747
user2720747

Reputation: 101

shouldStartLoadWithRequest is never called

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.

Upvotes: 9

Views: 17040

Answers (2)

Mani Kandan
Mani Kandan

Reputation: 629

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.

Upvotes: 14

Injectios
Injectios

Reputation: 2797

delegate method should be:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

And if you have UIWebView in xib, then probably you are trying to load request when UIWebView is nil

So make sure your myWebView instance exists.

Upvotes: 1

Related Questions