1

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

和另一个。如果网页被缓存意味着它从缓存加载,否则从实时 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];
    }


}
4

1 回答 1

1

更改此行

[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];

[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];

它会工作

于 2013-04-25T14:24:59.330 回答