我正在使用下面的代码将文本文件内容加载到WebView中,但每次调用该方法时,内容都需要一些时间来加载。我是否可以在此上下文中使用缓存策略来加快加载速度?
- (void)displayContent
{
@try {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"about-us" ofType:@"txt"];
[web_view loadHTMLString:[NSString stringWithContentOfFile:filePath encoding:NSUTF8StringEncoding error:nil] baseURL:nil];
} @catch(NSException e) {
NSLog(@"%@",e);
}
}
求助,
的Stephane
答案 0 :(得分:0)
为什么不将fileContents
字符串设置为nil
并使用主包中的资源内容“懒惰实例化”它?
例如,您的代码可以是:
- (void)displayContent
{
@try
{
if (fileContents == nil)
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"about-us" ofType:@"txt"];
fileContents = [NSString stringWithContentOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}
[web_view loadHTMLString:fileContents baseURL:nil];
}
@catch(NSException e)
{
NSLog(@"%@", e);
}
}
除非您有其他特殊问题,否则没有理由过度复杂化。