永远不会调用shouldStartLoadWithRequest

use*_*747 7 methods protocols ios uiwebviewdelegate

我已经研究过并且仍然不明白为什么从未调用过StartLoadWithRequest.我的页面加载正常,并调用了一些UIWebview委托协议方法.请从以下代码中找到相关的摘要:

在我的.m中合成我的webview(在头文件中定义):

@implementation PortViewController

@synthesize WebView = myWebView;
Run Code Online (Sandbox Code Playgroud)

我成功加载了我的webview:

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

    myURL = [NSURL URLWithString: myURLString];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [myWebView loadRequest:myRequest];
Run Code Online (Sandbox Code Playgroud)

把我的代表设置为自我

- (void)viewDidLoad
{


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

//myWebView.description.

myWebView.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)

我的所有协议方法都被称为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;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Man*_*dan 13

尝试在ViewWillAppear而不是ViewDidLoad中设置委托

-(void)viewWillAppear:(BOOL)animated
{
     myWebView.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)

并在您的PortViewController.m文件的界面中包含webview委托

@interface PortViewController ()<UIWebViewDelegate>


@end
Run Code Online (Sandbox Code Playgroud)

它应该工作.


Inj*_*ios 1

委托方法应该是:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Run Code Online (Sandbox Code Playgroud)

如果您UIWebView在 xib 中,那么您可能正在尝试在UIWebViewnil时加载请求

因此,请确保您的 myWebView 实例存在。

  • 这是任性的小写“w”,谢谢。我要去买眼镜了 (2认同)