2013-04-25 78 views
1

我有一个任务,提高网页加载速度的UIWebView。为此,我正在转向缓存概念。这里我使用ASIWebPageRequest来缓存网页的所有内容。它做得很好。但是当加载缓存的网页时,超链接不起作用(没有链接到实时网址)?在UIWebView加载缓存的网页在ios

和另一个。如果网页缓存意味着,它从缓存加载,否则从实时URL加载。我该如何解决它?

这里是我的代码:

- (void)fetchURL:(NSURL *)url 
{ 

    [self setRequestsInProgress:[NSMutableArray array]]; 

    [request setDelegate:nil]; 
    [request cancel]; 
    [self setRequest:[ASIWebPageRequest requestWithURL:url]]; 

    [request setDidFailSelector:@selector(webPageFetchFailed:)]; 
    [request setDidFinishSelector:@selector(webPageFetchSucceeded:)]; 
    [request setDelegate:self]; 
    [request setDownloadProgressDelegate:self]; 
    [request setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; 



    [request setDownloadCache:[ASIDownloadCache sharedCache]]; 
    [request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; 

    // This is actually the most efficient way to set a download path for ASIWebPageRequest, as it writes to the cache directly 
    [request setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request]]; 

    [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; 
    [request startAsynchronous]; 
} 


- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest 
{ 
    NSLog(@"fetch error = %@",[theRequest error]); 
} 

- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest 
{ 
    NSURL *baseURL; 
    if ([request isFinished]) { 


     baseURL = [NSURL fileURLWithPath:[request downloadDestinationPath]]; 


     //  // If we're using ASIReplaceExternalResourcesWithLocalURLs, we must set the baseURL to point to our locally cached file 
    } else { 
     baseURL = [request url]; 
    } 

    if ([theRequest downloadDestinationPath]) { 

     NSString *response = [NSString stringWithContentsOfFile:[theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil]; 

     [webView loadHTMLString:response baseURL:baseURL]; 


    } else { 

     [webView loadHTMLString:[theRequest responseString] baseURL:baseURL]; 
    } 


} 

回答

1

改变这一行

[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; 

[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES]; 

它将工作