UIWebView:加载某些 URL 时未调用 webViewDidStartLoad/webViewDidFinishLoad 委托方法

发布于 2024-10-06 04:28:06 字数 887 浏览 8 评论 0原文

我有使用 UIWebView 实现的基本网络浏览器。我注意到,对于某些页面,没有调用任何 UIWebViewDelegate 方法。

发生这种情况的示例页面为:http://www.youtube.com/user/google。以下是重现该问题的步骤(确保在控制器的 UIWebViewDelegate 方法中插入 NSLog 调用):

  1. 将上述 youtube URL 加载到 UIWebView 中> [请注意,页面加载时确实会调用 UIWebViewDelegate 方法]
  2. 触摸页面上的“上传”类别
  3. 触摸该类别中的任何视频 [问题:注意到加载了一个新页面,但没有调用任何 UIWebView 委托]

我知道这不是 UIWebView 委托未设置的问题正确的是,因为在加载其他链接时确实会调用委托方法(例如,如果您尝试单击将您带出 youtube 的链接,您会注意到委托方法被调用)。

我最初的直觉是,这可能是因为页面是使用 AJAX 加载的,这可能不会调用委托方法。但是当我检查iPhone的Safari时,它没有出现这个问题,所以它一定是我这边的问题。

我还注意到 Three20 的 TTWebController 与我遇到的问题完全相同。

但此问题产生的问题是,如果不调用委托方法,我无法在加载新请求时更新 UI 以启用/禁用后退和前进浏览按钮。

并了解为什么会发生这种情况,或者如何解决它以在发出新请求时更新 UI?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀中猫帐中妖 2024-10-13 04:28:06

这不是 iOS 错误 - 页面实际上并未重新加载。 UIWebView 委托会在新页面请求后触发,但该页面不会执行此操作。

仔细观察当您单击该页面上的视频链接(如您所描述)时,桌面版 Safari 中会发生什么情况。请务必注意地址栏。地址将会改变,但重要的是页面不会重新加载

这一切都是由 JavaScript 处理的,而不是通过重新加载页面来处理。简而言之,页面永远不会重新加载,因此没有理由调用 UIWebView 委托。

如果您不相信我,为了最终证明这一点,请尝试在禁用 JavaScript 的情况下重复您描述的步骤。您会注意到页面的行为完全不同。

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.

没有心的人 2024-10-13 04:28:06

这不是一个好的解决方案,但我使用 NSTimer 来更新按钮的状态:

- (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;
}

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;
}
笑梦风尘 2024-10-13 04:28:06

看起来这个问题在 iOS 4.2 中得到了修复。它适用于 iOS 4.2。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文