iOS how to remove existing UIWebView and reinitialise it - ios

I have a UIWebView which load a page when the user navigates he can go to 7 levels I mean he can go to 7 different pages with different URL.
Now I want to go back to the first page directly instead of going back one by one.
I couldn't find any API in UIWebView to do this so I thought to loading the first page url again but UIWebView's cangoback will return true.
So I thought I will reinitialise the webview and load the URL
self.webview=[self.webview init];
as expect it loads the new url and UIWebView's cangoback returns false which is good.
but the problem is when I try to scroll the page down I can see the previous page behind it as background of scrollview.
Any ideas on how to fix the issue

You're leaving the old web view alive, but just calling it's init routine (which you shouldn't do more than once). Instead, call:
self.webview = [[UIWebView alloc] init];
That will give you a whole new web page like you originally started with, and will invoke ARC to dispose of the old one.

Related

Any way to know before html content was shown in UIWebView

Is any possible ways that I would like to know before html content was shown in uiwebview.
coz when I called to some url, It will take a few seconds to complete the whole page loading. Before loading was completed, some of the content was already shown. I would like to know before that content was shown or before start showing.
UIWebView has a delegate protocol called UIWebViewDelegate. Two methods you should implement are – webViewDidStartLoad: notify monitoring object of when a site will about to load and – webView:shouldStartLoadWithRequest: notify when there a certain request like click, submit, back/forward etc are being made

Two Web Views, Two Tabs, Clicking a link in tab1/webview, opens the link in tab2/webview

The title says it all. And I've spent three days now trying to figure it out. I've used protocols and delegates. I simply am trying to pass data between tabs, while opening up the link in tab two. And I am very close to figuring it out, but my problem is right now, is that when I use:
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:1];
It switches to tab 2, but it doesn't open the link. I have physically click on tab 2, then physically click on tab 1, and then click the link.
I am ready to throw out the hours of time I've put into this, if someone can give me the easiest way to get my question fixed.
It's difficult to understand your question because of some of the terminology, but if I understand you correctly. The way I would solve passing a link from one UIWebview to another would be to utilize the delegate methods for UIWebview. Specifically, i'd get the url from the following call on the first UIWebview.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
(You can harvest the url from the NSURLRequest, or just pass along entire request)
and i'd pass it along to the newly loaded tabbarController and the new UIWebview. Using Something like the following:
[secondWebview loadRequest:requestSavedFromFirstWebView];
. It's unclear where exactly you are getting hung up.
As far as passing data from one tab in a UITabbar to another. We'd need more information on your setup (as was pointed out in comments). If you can set a public property for the request on your 2nd view controller then you can load it when the viewDidAppear method gets called.

How can I close UIWebView after submitting form in the view?

I have a UIWebView that displays a webpage including a feedback form. I want to remove this UIWebView after I submit the form in that view.
How can I do that?
You normally achieve this sort of thing by setting a delegate on the web view, to register for interesting callbacks.
Don't forget to nil the delegate before you dispose of the web view or you might experience a crash.
In the delegate for the UIWebView, within
webView:shouldStartLoadWithRequest:navigationType:
search for a the url triggered by the form submission. If it's present, set a flag value.
Then, in your delegate's webViewDidFinishLoad: method, if the flag is set, remove the webView.
Well You Have not made clear of your allocation of your webview or not familiar with your code .I guess you want to return to previous view from the UIWebView , in that case you can simply the push the viewcontroller containing the webview through a UINavigationController and get back to the view on back button or set a flag in webviewDidStartLoad and webViewDidFinishLoad and remove the view when the flag is set in DidFinish Part else please elaborate your problem.

Ensuring memory not leaking in UIWebView

I would like to know if I am using good memory management practise with UIWebView. My app makes extensive use of hundreds of local html files, which the user can get to at the end of a table hierarchy. The user can also swipe left and right for previous and next pages.
In viewDidLoad, I set everything up, such as background colour and gesture recognizers, and I add it to the view:
[self.view addSubview: self.myWebView];
Only in dealloc do I set the UIWebView's delegate to nil, and release it.
In viewWillAppear, I set the UIWebView's delegate to self, while in viewWillDisappear, I set the delegate to nil (as well as ask it to "stopLoading").
Each time I load a new document in, I use loadHTMLString:
[self.myWebView loadHTMLString:fullHTML baseURL:nil];
Is what I'm doing sufficient to keep the memory happy? Is this issue from two years ago still relevant?
Thank you!
What you have written should not leak memory. I don't know if the issue you linked to still happens, but I have not had problems with it in my use of UIWebView.

Clear UIWebView content upon dismissal of modal view (iPhone OS 3.0)

I currently have a UIWebView that is displayed within a modal view. It is basically a detail view that provides a view of a page when the user clicks a link. When the view is dismissed and then brought up again (when the user clicks another link), the previously-loaded content is still visible and the new content loads "on top" of the last content. This makes sense because the instance of the UIWebView persists between sessions and is only released when the memory is needed.
However, I would like to completely clear the UIWebView when the modal view is dismissed so that 1) content is cleared and 2) memory is freed. Thus far my research and attempts have not found an answer. These links haven't worked for me:
is it possible to free memory of UIWebView?
Reused UIWebView showing previous loaded content for a brief second on iPhone
I've tried [[NSURLCache sharedURLCache] removeAllCachedResponses]; and setting the webView to nil and manually releasing the webView upon modal-view-dismiss to no avail. Any thoughts from the wizened masses?
Sometimes loadHTMLString is too slow. You also can use:
[webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML = \"\";"];
I'm not sure if that will free up much memory though...
Releasing the web view is probably the best approach. If a view is in a view hierarchy (has a superview) you must call removeFromSuperview to get the superview to release the view.
You could also load an html string for an empty document:
[webView loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
It seems under some circumstances the methods suggested by drawnonward and rob may not work (possibly something to do with CSS on the page that's being cleared), however this did work :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
evaluating javascript is usually quicker than loading a new request.
[webview stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
In my case I needed to clear the web view from its contents instantaneously and be able to start loading something else right away. Neither loading an empty document HTML string nor clearing the page with JavaScript resulted in desired effect. Instead I used:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://stackoverflow.com"]]];
And this cleared the view to the default background and initiated next page's loading as expected.
I hope this helps someone else as well.
i prefer to the following:
[m_webView stringByEvaluatingJavaScriptFromString:#"location.replace('about:blank')"]

Resources