I need to clean the content of UIWebView(in order to reuse it), but I have some authentication handling code in its delegate methods, so I do not want to load an empty page like about:blank to clean it, as it will trigger my authentication handling code. So is there a way for doing this?
(also, by reusing it, I need to put a spinner on top of the web view, and when it loads another page, I don't want the user see the previous loaded page content, so that's why I need to clean it)
Thanks!
You can just use this line of code :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
Here you will find whole demo
this did the trick for me:
[webView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
Try this out. This worked for me
[self.webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML = \"\";"];
If your only problem is that the spinner disappears when you load a blank page, you can use this code in webViewDidFinishLoad:
– (void)webViewDidFinishLoad:(UIWebView *)currentWebView {
if (![currentWebView.request.URL.description isEqualToString: #"about:blank"]) {
self.mySpinner.hidden = YES;
}
}
...then the spinner will go on until your webview is actually done loading.
Related
Some help please,
I am putting a web app together and I am stuck on a few things.
I have tabviewcontrollers which load different uiwebviews.
Each time I navigate on the app and re-click the tab it remains where I was on that page is there a way to re-load it so it always goes from the orginal ur (not just a refresh)?
Any advice on the best way to handle this would be appreciated.
Thank you
Steve
Load the request in your view controller's viewWillAppear: method.
When you want to refresh, use
[webView reload]
or for if you want t reload with a specific url
NSURL *theURL = [NSURL URLWithString:#"http://urltoreload.html"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
I'm making an iPhone app using xcode, but whenever the homepage (localHTML) is visited using the backbutton, the list item stays highlighted. My solution to this is to refresh the page whenever the localHTML is visited. Can anyone suggest an easy way of detecting when the url matches localHTML (should I use isFileURL?) Thanks for your help. Also, the list was made with javascript or else I would use deselectRowAtIndexPath.
This is what I came up with, but it doesn't work
-(IBAction)backbutton{
[webView goBack];
NSString *currentURL = webView.request.URL.absoluteString;
if (currentURL = "whatever the file path is") {
[webView reload];
}
is this a valid way to accomplish this? is there a way to get my localhtml's file path by using nslog?
Have you tried having a delay after going back, then refreshing?
eg. after
[webView goBack];
have a delay now using nstimer
then
[webView reload];
hi i guess this is pretty simple and hope it will be answered in a easy manner!
how do i change the url of the uiwebview when a button is touched up inside?
meaning to say i will have a single web view and everytime a button is clicked the URL of the webview
changes and a new page is reloaded
thanks
As you suggest, it's very easy:
-(void)buttonPressed:(id)sender {
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://website.com/"]]];
}
This is all very well documented.
In one of my apps I reuse a webview. Each time the user enters a certain view on reload cached data to the webview using the method :-
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL
and I wait for the callback call
- (void) webViewDidFinishLoad:(UIWebView *)webView.
In the mean time I hide the webview and show a 'loading' label.
Only when I receive webViewDidFinishLoad do I show the webview.
Many times what happens I see the previous data that was loaded to the webview for a brief second before the new data I loaded kicks in.
I already added a delay of 0.2 seconds before showing the webview but it didn't help.
Instead of solving this by adding more time to the delay does anyone know how to solve this issue or maybe clear old data from a webview without release and allocating it every time?
Thanks malaki1974, in my case I wasn't using a modal view.
When I sat with an Apple engineer on WWDC 2010 and asked him this question his answer was simply: "Don't reuse UIWebViews, that's not how they were ment to be used."
Since then I make sure to calls this set of lines before allocating a new UIWebView
[self.myWebView removeFromSuperview];
self.myWebView.delegate = nil;
[self.myWebView stopLoading];
[self.myWebView release];
That solved the issue.
Clear the contents of the webview before you try to load new content
[self loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
First, the UIWebView renders it contents in a background thread. Even when you receive webViewDidFinishLoad: it might not be completely done. Specially if it is an ajax-intense page that comes from the network.
You say you are hiding the view. I wonder if that means that the webview delays its drawing completely. What you could try is to move the UIWebView offscreen or obscure it with another view. Maybe that will change it's drawing behaviour.
If you do not need an interactive UIWebView then you can also consider to do it completely offscreen in a separate UIWindow and then create an image from that UIWebView's layer.
That's what I do, and it works:
[_webView stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
Try loading a local file that is blank or has a loading graphic when you hide it, rather than just loading new content when you show it. Since the file is local it will be quick and even if the new page takes a while to load it will have either blank or loading expected behavior.
If you got controll over the html. You can communicate back to objective-c when the document is ready. Like so in jQuery:
function messageNative (name, string) {
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "appscheme://" + name + "/" + string);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
$(function() {
messageNative('webview', 'ready');
});
And then in UIWebView's delegate method webView:shouldStartLoadWithRequest:navigationType: wait for the request with url equal to "appscheme://webview/ready". Then you should know: the document is loaded and ready for display. Then all that is missing is a simple fade-in or something like that :)
I'm trying to transition between loading of different web pages by hiding the webView while it is loading a page. However, I'm seeing that some image intensive websites are causing webViewDidFinishLoading to fire too soon and when I show the webView at that point then for a split second you get a view of the previous page. Any ideas on how to resolve this?
If there's Javascript on the page, you may need to wait for it to finish. The easiest way seems to be to send some javascript to the page to be executed:
-(void) webViewDidFinishLoad:(UIWebView *)webView
{
NSString *javaScript = #"<script type=\"text/javascript\">function myFunction(){return 1+1;}</script>";
[webView stringByEvaluatingJavaScriptFromString:javaScript];
// done here
}
Having said that, I seem to still see cases where the webview isn't quite updated within webViewDidFinishLoad.
I've encountered this problem as well. Although I haven't found a solution, I've worked around the problem by introducing a 0.5 second delay before showing the UIWebView once the webViewDidFinishLoading delegate method is called.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self performSelector:#selector(displayWebView) withObject:nil afterDelay:0.5];
}