【问题标题】:How to make WebView content load faster?如何让 WebView 内容加载更快?
【发布时间】:2012-01-08 03:46:22
【问题描述】:

我正在使用下面的代码将文本文件内容加载到 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);
   }
}

感谢您的帮助,

斯蒂芬

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4 uiwebview


    【解决方案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);
        }
    }
    

    除非您有其他特别的顾虑,否则没有理由让事情变得过于复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-03
      • 2021-06-08
      • 2015-08-04
      • 2021-10-13
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多