How to reload a UIWebView? - iphone

I want to reload a UIWebView (including all labels and images) when a button is tapped. How can I do that?

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[webView reload];
}
or
NSURL *theURL = [NSURL URLWithString:#"http://www.OurLovingMother.org/Mobile.aspx"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}

- (IBAction)buttonClicked
{
[yourWebview reload];
}
Hope this helps

Note that if you load the content of your UIWebView using loadHTMLString: then you'll need to call loadHTMLString: again to reload it. In this case the reload method doesn't work.

You can try
[webView reload];
in your click handler.

Related

How can i clear web view on switching from one class to another?

i have a webview in my detailviewcontroller to load html formatted local text to show details, detailviewcontroller also has a back button, that when pressed i want to clear the web view so that i other detail will show it will be clear to avoid a flick of reloading.
in DetailViewController i have-
- (IBAction)backbuttonClicked:(id)sender {
[_delegate backFromDetailView];
}
-(void)clearWebView{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.detailWebView loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
[self.detailWebView reload];
}
-(void)changeDetailViewTextTo:(NSString *)text{
NSString *htmlString=[HTMLString getHTMLStringForString:text];
[self.detailWebView loadHTMLString:htmlString baseURL:nil];
}
and in mainviewcontroller i have
-(void)backFromDetailView{
((DetailViewController*)_detailsViewController).delegate=nil;
[((DetailViewController*)_detailsViewController) clearWebView];
}
Still flicking the text, please help.
you can load your webview with the default blank page.
[self.detailWebView loadRequest:
[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];

UIWebView getting crashed

I have a UIWebView which will load the documents. All the supported documents mentioned for webview it is working. But sometimes , the UIWebView is getting crashed whenever I move back and forth quickly. I mean I am clicking back and opening file again and again. If I do so , my app is getting crashed.
Any idea why this is happening ?? It is working cool for video,audio and txt files, but weird crashing for the Office docs and numbers,pages documents.
Please help me out !!
-(void) backBtnPressed
{
[_webView stopLoading];
if ([_webView superview])
{
[_webView removeFromSuperview];
self.navigationItem.rightBarButtonItem = nil;
}
}
- (void) loadFile:(NSString*)filePath
{
NSURL* url = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
}
Set the delegate property of the webview to nil in the dealloc of your controller
If you are moving from one view controller to another, do the following
[webView stopLoading];
webView.delegate = nil;
Implement this and chk what error you are getting.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog("Error=%s",error);
}
The problem is solved.Setting the UIWebView to nil and loading the request again in the WebView. But now only at most worst case the App is getting crashed.
Try this :
-(void)viewWillDisappear:(BOOL)animated
{
if([webView isLoading])
{
[webView stopLoading];
}
}
Just adding this here on the off chance that someone else is making the same beginner mistake:
Remember that webView.delegate is a weak reference. If you set it to something else than self your delegate may get destroyed if there are no other references to it, which will cause all kinds of crashes.
Try to do the following. It works for me.
[webView stopLoading];
[webView release];
webView = nil;
webView.delegate = nil;

reload a UIWebView

I'm using a UIWebView like that in the viewDidLoad:
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; // init and create the UIWebView
webView.autoresizesSubviews = YES;
webView.userInteractionEnabled = NO;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webView setDelegate:self];//set the web view delegates for the web view to be itself
NSURLRequest *requestObject = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]];//Create a URL object & Set the URL to go to for your UIWebView
[webView loadRequest:requestObject];//load the URL into the web view.
[self.view addSubview:webView];//add the web view to the content view
[webView release], webView = nil;
When I try to change the page, I try that BUT it doesn't working :
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
The code that's just above is in the action of a TabBar button.
**EDIT :
When I comment
[webView release], webView = nil;
I can see the apple website but not facebook when I push the button! The screen is refreshing in the apple website and not facebook...**
Hundred of thanks to help ;-)
You've released and set your webView to nil. So the object no longer exists. You are calling loadRequest and reload on a nil object which will do nothing.
Assuming you have webView as an instance variable in your class, remove the line
[webView release], webView = nil;
and instead put that in your dealloc method otherwise when you call
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
then you will be calling it on a nil object which will do nothing.
Where did you try this code ?
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
You already released webview and make it Nil. So it will not work if you did it after it loads www.apple.com.
[webView release], webView = nil;
Remove above two lines once and then try. It should work.
Hope this help.
The potential problem I see is that you are setting the webView value to nil after adding it as subview, but before doing the reload:
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
here webView is nil... the object still exists (because it is embedded in a view), but the variable does not point to it anymore, so messages are simply ignored.
no need of reload
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
}
this may help..!!
Reading your code, I guess that the your webView is declared as #property in the header file. You release this object after the Apple site is loaded. I think that's the reason why it doesn't load the facebook website.
The [webView release]; should be placed within the - (void) dealloc method only.
Please let me know if it works!

What's the best option for loading web content in a UIWebView?

In previous apps, I've loaded data into a UIWebView using 4 steps:
1. Use an NSURL Connection to grab the data from the web asynchronously
2. WHen the download is complete, convert the NSData object to a string, and manipulate the data prior to display.
3. Write the converted data out to the doc folder
4. Load the data into the UIWebView from the file
But in my current app, I have no need to manipulate the data that I load from the web. I simply want to download asynchronously, and load it into a UIWebView while that view is not yet visible. Then show that view later, when I need it.
Would it be better to use a loadRequest message that was dispatched as a block to GCD? Is that workable? I'm trying to avoid the whole mess of writing to a local file, then reloading the page from that file. Suggestions?
Why not just give the NSURLRequest directly to the webview ?
It's perfectly capable of loading data from the web itself if you don't need to massage the data in transit.
Try like this
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
All the best.
UIWebView
You can load the request like #Warrior has mentioned
The main advantage is its loaded asynchronously and you don't have to manage everything like #Kevin Ballard has mentioned.
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
UIWebViewDelegate
But if you want to be notified when the url is load completely.You have to conform to the UIWebViewDelegate
Header
#interface YourController : UIViewController <UIWebViewDelegate>
Implementation
1.you have to set the delegate to self
self.yourWebView.delegate = self;
2.you have to implement the following delegate that is where you will be notified when the request loading is completed.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Do whatever you want here
}
3.you have to set webview delegate to nil and stop loading the request and to prevent yourself from notorius EXC_BAD_ACCESS Bug due dangling pointer.You can find more about here
// If ARC is used
- (void)dealloc {
[_webView setDelegate:nil];
[_webView stopLoading];
}
// If ARC is not used
- (void)dealloc {
[webView setDelegate:nil];
[webView stopLoading];
[webView release];
[super dealloc];
}
// ARC - Before iOS6 as its deprecated from it.
- (void)viewWillUnload {
[webView setDelegate:nil];
[webView stopLoading];
}
Note:
You should not embed UIWebView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
I hope this covers most needed basic things

Cannot load alternating urls with UIWebview

I have one view holding a list of videos(buttons which can be clicked, each hold a url) which currently bring up a new view which holds the UIWebview, the only problem is I cant pass the url to the webview depending on which button was clicked.
At the moment I just get an empty UIWebview.
How can the url string be passed to the webview so it can load the correct url for each button?
Regards
NSURL *videoURL = [NSURL URLWithString:#"http://..."];
[webView loadRequest:[NSURLRequest requestWithURL:videoURL]];
UPDATE:
In response to comment #3, here's what you can do:
This goes without saying, but keep a reference to the UIWebView instance in Browser
Add an NSString or NSURL #property to Browser
Pass the URL to the Browser instance right after init
In viewDidLoad:, call loadRequest:
So your code might look like this:
- (void)buttonClicked {
Browser *browser = [[Browser alloc] initWithNibName:nil bundle:nil];
browser.URL = [NSURL URLWithString:#"http..."];
[self presentModalViewController:browser animated:YES];
[browser release];
}
and in Browser's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
}