【问题标题】:loading cached webpage in UIWebView in ios在ios的UIWebView中加载缓存的网页
【发布时间】:2013-04-19 06:59:33
【问题描述】:

我的任务是提高 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];
    }


}

【问题讨论】:

    标签: ios6 uiwebview asihttprequest uiweb


    【解决方案1】:

    改变这一行

    [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
    

    [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
    

    会有用的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2012-06-13
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多