问题
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:
UIWebViewDelegate
for all the power it should handle, is very terse and has next to none documentation. Can anyone confirm thatdidStartLoad
/didFinishLoad
is really called only for "frames" which means most probably "frameset/frame" and "iframe" ?- how can i distinguish the above DOM-induced loads from request redirections (which produce the events too)?
- my observation is that each
didStartLoad
is preceded withshouldStartLoadWithRequest
, except redirections. Is this a dependable behavior? - 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:
- I can't.
- I don't know.
- I don't know.
- 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