问题
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