gpt4 book ai didi

ios - 如何使用EGOCache更快地缓存和加载页面

转载 作者:行者123 更新时间:2023-12-01 16:04:34 25 4
gpt4 key购买 nike

我的iOS应用程序中有一个UIWebView,它根据先前的操作加载不同的URL。我不希望这些页面尽快加载。我发现了类EGOCache(source),我知道它可以将cacheData存储在library / Caches目录中。但是我不确定如何检索此缓存以更快地加载它,我看不出有什么区别。也许使用NSCache?我错过了什么?

- (void)webViewDidStartLoad:(UIWebView *)webView {
if (webView_1) {


NSString *urlAddress = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[webView1 loadRequest:request];

NSData *data0 = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];

[[EGOCache globalCache] setData:data0 forKey:@"webCache"];
}
}

谢谢!

最佳答案

好的,所以我制定了自己的方法,并使其起作用。您所要做的就是提供网址。参见以下代码:

-(void)loadPage:(NSString *)urlAddress {

NSURL *url = [NSURL URLWithString:urlAddress];
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [cachePath stringByAppendingPathComponent:@"/xxx.APPNAME/EGOCache/EGOCache.plist"];
NSDictionary *dict =[NSDictionary dictionaryWithContentsOfFile:file];

if ([dict objectForKey:urlAddress] )
{
NSData *data = [[EGOCache globalCache] dataForKey:urlAddress];
data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
NSLog(@"loading from cache %@",urlAddress);

}else{
NSData *data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
[[EGOCache globalCache] setData:data forKey:urlAddress];
NSLog(@"saving cache %@",urlAddress);
[[EGOCache globalCache] setDefaultTimeoutInterval:60*60*24*4]; //timeout (4 days)
}
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];

[webView loadRequest:request];
}

关于ios - 如何使用EGOCache更快地缓存和加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21242560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com