reload a UIWebView - iphone

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!

Related

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;

How to reload a UIWebView?

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.

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

How to delete the cache from UIWebview or dealloc UIWebview

Every time I load a new page with UIWebView the before loaded page is shown for a short time.
How can I clear that cache? Another possibility would be to dealloc UIWebview. I tried that but than my UIWebView is always "empty". How should the alloc and dealloc be done in this case?
I noticed that the UIWebView is consuming about 10 MB RAM. Now the UIWebView is loaded together with the ViewController. And the view is autoreleased as well as the UIWebView is autoreleased. Wouldn't it be better to dealloc the WebView each time?
Solution:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 0, 320, 480);
self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];
self.webView.scalesPageToFit = YES;
[self.view addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.webView removeFromSuperview];
self.webView = nil;
}
I had nearly the same problem. I wanted the webview cache to be cleared, because everytime i reload a local webpage in an UIWebView, the old one is shown. So I found a solution by simply setting the cachePolicy property of the request. Use a NSMutableURLRequest to set this property. With all that everything works fine with reloading the UIWebView.
NSURL *url = [NSURL fileURLWithPath:MyHTMLFilePath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[self.webView loadRequest:request];
Hope that helps!
My solution to this problem was to create the UIWebView programmatically on viewWillAppear: and to release it on viewDidDisappear:, like this:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.webView = [[[UIWebView alloc]initWithFrame:exampleFrame] autorelease];
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
self.webView.autoresizingMask = webViewBed.autoresizingMask;
[exampleView addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
}
If you do this and your UIWebView doesn't get released you should check it's retain count and understand who is retaining it.
If you want to remove all cached responses, you may also want to try something like this:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
If I had to make a guess your property for webView is
#property (nonatomic, retain) IBOutlet UIWebView *webView;
Correct? The important part is the retain.
self.webView = [[UIWebView alloc]initWithFrame:frame];
The above code allocates a UIWebView (meaning retainCount = 1) and then adds it to self.webView, where it is retained again (retainCount = 2).
Change the code to the following, and your webView should be deallocated.
self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];
addSubview: can only be called on UIViews.
//[self.navigationController addSubview:self.webView];
should be
[self.navigationController.view addSubview:self.webView];
I did something simpler, I just set the webView's alpha to 0 on loadRequest and 1 on webViewDidFinishLoad.
thewebview.alpha=0.0;
thewebview.backgroundColor=[UIColor whiteColor];
[thewebview loadRequest:[NSURLRequest requestWithURL:url(str)]];
and
- (void)webViewDidFinishLoad:(UIWebView *)webView {
thewebview.alpha=1.0;
}
(Actually I have a fade-in, as rendering f.ex. a PDF can take 1/5 seconds or so.)
You could also load #"about:blank" after the webview disappears, then it will be empty the next time you access it.

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]];
}