iOS UIWebView page load observability

2019-07-11 06:58发布

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.

2条回答
Melony?
2楼-- · 2019-07-11 07:44
  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)

查看更多
The star\"
3楼-- · 2019-07-11 07:55

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.

查看更多
登录 后发表回答