Reputation: 474
I am finding a strange behaviour of UIWebView.
In the delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self changeFont];
//[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeFont) userInfo:nil repeats:NO];
}
In changeFont, I am calling the javaScript to change font of web page. It wont get applied but if I call change font method after a delay of few second from delegate webViewDidFinishLoad then it will be applied. if files are large how can we determine how much time webview will take to load if we have to fire timer from delegate webViewDidFinish to apply font size and style for file.
any explaination for this. I serached a lot and nothing is mentioned in docs.
Any help why such behaviour of UIWebView?
Upvotes: 1
Views: 1976
Reputation: 331
Apple's Document explains that webViewDidFinishLoad
and webViewDidStartLoad
will be called when finished/starting loading a frame
So after the webViewDidFinishLoad
called(when the basic html is loaded), there could be JavaScript or Ajax in the page still executing. You need to wait for all of them done.
I would suggest two ways to try:
the later one is more powerful when dealing with custom events. And this question i answered may help:
Upvotes: 1
Reputation: 5
I think it is because web page may not be rendered fully (I mean all javascript, resources and so on) at the moment when - (void)webViewDidStartLoad:(UIWebView *)webView
method fired
This explains why your method works second time after some delay..(page is become fully loaded)
You can use callback from javascript to objective-c to determine rendering process ended or not, more information about this feature (or little hack) you can find at the section: "Javascript communicating back with Objective-C code" by link http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/.
or use stringByEvaluatingJavaScriptFromString:
- I've provided an example of using it in another related topic https://stackoverflow.com/a/8560489/857417
Upvotes: 0
Reputation: 16327
Try calling the "changeFont" method on the main thread. Like [self performSelectorInMainThread:...];
Upvotes: 0