shouldStartLoadWithRequest is never called

≡放荡痞女 提交于 2019-12-22 05:07:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18460623/shouldstartloadwithrequest-is-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!