开发者

UIWebView: webViewDidStartLoad/webViewDidFinishLoad delegate methods not called when loading certain URLs

开发者 https://www.devze.com 2023-01-27 23:22 出处:网络
I have basic web browser implemented using a UIWebView. I\'ve noticed that for some pages, none of the UIWebViewDelegate methods are called.

I have basic web browser implemented using a UIWebView. I've noticed that for some pages, none of the UIWebViewDelegate methods are called.

An example page in which this happens is: http://www.youtube.com/user/google. Here are the steps to reproduce the issue (make sure you insert NSLog calls in your controller's UIWebViewDelegate methods):

  1. Load the above youtube URL into the UIWebView [notice that here, the UIWebViewDelegate methods do get called when the page loads]
  2. Touch the "Uploads" category on the page
  3. Touch any video in that category [issue: notice that a new page is loaded, but none of the UIWebView delegates are called]

I know that this is not an issue of UIWebView's delegate not being set properly, since the delegate methods do get invoked when loading other links (e.g. if you try clicking on a link that takes you outside of youtube, you'll notice the delegate开发者_开发技巧 methods getting called).

My gut feeling initially was that it might be because the page is loaded using AJAX, which may not invoke the delegate method. But then when I checked the iPhone's Safari, it did not exhibit this problem, so it must be something on my side.

I've also noticed that Three20's TTWebController has the exact same issue as I'm having.

But the problem that arises from this issue is that without the delegate methods called, I'm unable to update the UI to enable/disable the back and forward browsing buttons when new requests are loaded.

And idea why this is happening or how can I work around it to update the UI when a new request is made?


This isn't an iOS bug - the page isn't actually reloading. The UIWebView delegates are triggered following new page requests, but that page doesn't do that.

Look very carefully at what happens in desktop Safari when you click the video link on that page as you describe. Make sure you pay attention to the address bar. The address will change, but critically the page will not reload.

This is all handled by JavaScript, not by reloading the page. Simply put, the page never reloads, so there's no reason for the UIWebView delegates to be called.

If you don't believe me, to conclusively prove this try repeating the steps you describe with JavaScript disabled. You'll notice the page behaves completely differently.


this is not good solution but im using NSTimer for updating status of buttons:

- (BOOL)webView:(UIWebView *)_webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (!_timer && [request.URL.absoluteString rangeOfString:@"youtube.com"].length != 0) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                  target:self
                                                selector:@selector(checkNavigationStatus)
                                                userInfo:nil
                                                 repeats:YES];
    }

    return YES;
}

//....
//....
//....

- (void)checkNavigationStatus
{
    // Check if we can go forward or back
    backButton.enabled = self.webView.canGoBack;
    forwardButton.enabled = self.webView.canGoForward;
}


Looks like this got fixed in iOS 4.2. It works in iOS 4.2.

0

精彩评论

暂无评论...
验证码 换一张
取 消