2011-11-28 85 views
0

我正在使用下面的代碼將文本文件內容加載到WebView中,但每次調用該方法時,內容都需要一些時間加載。任何關於緩存策略的想法,我可以在這種情況下使用它來加快速度?如何讓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); 
    } 
} 

THX幫助,

斯特凡

回答

1

爲什麼不設置一個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); 
    } 
} 

沒有理由過分複雜化的東西,除非你有其他的特別關注。