简体   繁体   中英

shouldStartLoadWithRequest delegate method never called

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.

shouldStartLoadWithRequest should do the trick, but that method is never called when I click to a link. I cannot find why this doesn't work.

I have read somewhere that I need to add this line :

webView.delegate = (id)self;

I tried it, and still get the same issue. Please help

AppDelegate.m

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString] ;

    NSLog(@"Url: %@", currentURL);
    return YES;
}

Controller.m

- (void)viewDidLoad
{
    webView.delegate = (id)self;
    NSString *tokenString = @"123";
    [super viewDidLoad];

    NSString *fullURL = [[NSString alloc] initWithFormat:@"http://192.168.1.69:8888/webapp/test.php?uid=%@", tokenString];
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}

If your Controller instance adds itself as the delegate then the method you add to the AppDelegate instance will never be called.

Move the shouldStartLoadWithRequest method into your Controller class.

You've set your view controller as the web view's delegate. But then you've implemented the UIWebView delegate method in our application's delegate instead.

Try implementing that delegate method in your view controller. Your view controller is your web view's delegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM