Search code examples
iphoneuiwebviewnsurlbrowser-history

Save history of UIWebView and reload it


I have a view with a subview of UIWebView and the view is obviously managed by a view controller. Say, I browse with this UIWebView for sometime and then navigate to a different view managed by a different view controller and then return to the UIWebView view. It will load the parent URL and not the one where I had left it. How can I load the URL where I had browsed till along with the history i.e., ability to go back to the parent URL from the URL where I had switched views. The code might help.

NSString *string = [[NSString alloc] initWithString:@"http://www.google.com"];
NSURL *url =[[NSURL alloc] initWithString:string];
NSURLRequest *aRequest = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:aRequest];

When we come back to this web view, it will load 'url' again. But, I want it to load the url where the UIWebView was last at. Thanks.


Solution

  • It is really simple. On viewWillDisappear method, save the current URL of the webView.Request, on next launch, try to see if it has value and load the one existing, otherwise, load the default "google.com". Hope this helps.