iOS UIWebView page load observability

℡╲_俬逩灬. 提交于 2019-12-11 01:14:31

问题


I would like to know for sure, when a page load is finished in UIWebView. I am aware of the "refcounting" approach between didStartLoad/didFinishLoad outlined in here and it kinda-sorta works for me, but i don't feel very confident about it. It has grown some hairy ifs and still acts strangely when Javascript decides to talk into document.location. I am unclear namely about the following:

  1. UIWebViewDelegate for all the power it should handle, is very terse and has next to none documentation. Can anyone confirm that didStartLoad/didFinishLoad is really called only for "frames" which means most probably "frameset/frame" and "iframe" ?
  2. how can i distinguish the above DOM-induced loads from request redirections (which produce the events too)?
  3. my observation is that each didStartLoad is preceded with shouldStartLoadWithRequest, except redirections. Is this a dependable behavior?
  4. am i missing something, or is there no way of knowing what request has originated the particular didStartLoad? Because [[webView request] URL] of the only context given, is always returning the same URL, that is the one which UIWebView was asked to load initially.

回答1:


  1. I can't.
  2. I don't know.
  3. I don't know.
  4. I can't tell.

Useful answer, right?

Clearly, I think UIWebViewDelegate could (should?) be a little more expressive about what's going on in the UIWebView.

My attempts to observe the UIWebView activity were:

  • adding an observer on loading with iOS 6: the observer was never called.
  • trying that:

Delegate methods samples:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (!webView.loading) {
        [self.activityIndicator stopAnimating];
    }
}

which works quite well.

I think you should try this code with the JavaScript and iframe tricks you're mentioning. It it's not helping but that you own the JavaScript you're talking about, you could also consider sending signals to your app to indicate further loading actions are in progress (like calling URL with custom scheme like myApp://didStartLoadingSomethingWithJavascriptDude and myApp://didFinishLoadingSomethingWithJavascriptDude)




回答2:


With UIWebViewNavigationType from shouldStartLoadWithRequest at least you can identify if the current loading comes from a user click or anything else which happened whithin the page. It can't help to know when the page is definitely loaded, but you can launch some code before a new one is triggered by the user. It may help, depending on the nature of task you have to do between two pages.



来源:https://stackoverflow.com/questions/15343516/ios-uiwebview-page-load-observability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!