Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 688
user2720747 Avatar asked Aug 27 '13 08:08

user2720747


2 Answers

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.

like image 123
Mani Kandan Avatar answered Nov 05 '22 14:11

Mani Kandan


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.

like image 41
Injectios Avatar answered Nov 05 '22 14:11

Injectios