Silver Challenge - How to clear the cache?


#1

Hi

My silver challenge solution…
In WebViewController.h I conformed to the protocol and added the following ivars:

UIWebView *wv; UIBarButtonItem *webBackButton; UIBarButtonItem *webForwardButton;
Then in WebViewController.m, in the loadView method I set the delegate ([wv setDelegate:self]:wink: and added:

UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; NSMutableArray *tbItems = [[NSMutableArray alloc] init]; webBackButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:101 target:self action:@selector(webBack)]; webForwardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:102 target:self action:@selector(webForward)]; [tbItems addObject:webBackButton]; [tbItems addObject:webForwardButton]; [tb setItems:tbItems animated:NO]; [self.view addSubview:tb];
Then I add the webViewDidFinishLoad method to enable and disable the back and forward buttons:

- (void)webViewDidFinishLoad:(UIWebView *)webView { // Enable/disable back [webBackButton setEnabled:[wv canGoBack]]; // Enable/disable forward [webForwardButton setEnabled:[wv canGoForward]]; }
And then added the methods for these 2 buttons:

[code]- (void)webBack
{
[wv goBack];
}

  • (void)webForward
    {
    [wv goForward];
    }[/code]

I also added:

[code]- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear];

[wv setDelegate:nil];

}[/code]as the documentation seemed to suggest the delegate would be retained (not sure if doing this right or is needed at all).
Works Ok - but once you’ve been on a page, then returned to the table view, then returned back to the web view, the previous pages have cached and if click back then you can’t click forward!

Is there any way of clearing the cache each time the web view loads? I’ve tried [[NSURLCache sharedURLCache] removeAllCachedResponses]; in the ListViewController.m and tried adding [wv removeFromSuperview]; into the viewDidDisappear method - this one stopped any caching at all - even when navigating within a web view.

Any ideas?

Thanks, Mark


#2

You mean clear the history? AFAIK, the easiest way to do this is to just destroy the web view and create a new one.

I’m pretty sure there is some JavaScript code to send to the web view to clear it too, but I don’t know any of that silly web stuff.


#3

Thanks Joe

Yes sorry - I meant clear history!

For this challenge is it therefore acceptable behaviour for the back arrow button on the web view to go to the previous page from a different selected post in the list view? For example, this is how my solution current behaves; Launch app > select a post > opens in web view with forward and back buttons not enabled (as no previous history) > click any link on the loaded post page (back button becomes enabled as you would expect) > click the Back button in the navigation bar to return to the list view of posts > select another post > back arrow button in web view is now enabled and clicking it goes back to last page loaded from the previous web view.

I was trying to have both back and forward arrows on the web view disabled each time you load a new post - is this not necessary for this challenge?

Thanks, Mark


#4

If you change the code to create a new instance of webviewcontroller each time the user selects a cell, then you won’t have to worry about the webview’s history carrying over. (The book has you use a property for the webview for later in the book but you just get rid of it along with the webview’s initialization in the app delegate).