加速加载WKWebView

Ran*_*lue 5 performance startup ios ios8 wkwebview

正如解释在这里,WKWebView有一个bug,从而捆绑本地的网页应用程序都捆绑复制到tmp目录中.我将包复制到的代码tmp是:

// Clear tmp directory
NSArray* temporaryDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in temporaryDirectory) {
    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"build"];
NSString *temporaryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"build"];
NSError *error = nil;

// Copy directory
if(![fileManager copyItemAtPath:sourcePath toPath:temporaryPath error:&error]) {
    [self logError:@"Could not copy directory" :error];
}
Run Code Online (Sandbox Code Playgroud)

我已经定时了这个特定的代码片段,它占用了我应用程序启动时间的50%!(约0.5s,总共~1s.)

有没有办法在iOS 8下加速(或完全避免)这个特定的代码片段?线程可以帮忙吗?

roo*_*oop 5

鉴于您的资产足够大,需要花0.5秒才能复制,我的建议是采用稍微不同的方法来加快(明显的)启动时间:

  1. 使用占位符divs 创建一个普通的旧HTML页面来加载资源(如果您愿意,可以在base64中包含小资产(例如加载指示符图像))
  2. 加载HTML后(可能打开webView:didFinishNavigation:),将副本启动到build文件夹
  3. 复制完成后,使用javascript(with evaluateJavaScript:completionHandler:)使用您的资产填充占位符div

虽然我意识到这并没有直接解决这个问题,但我认为在这种情况下,方法的微小改变最好.


sof*_*are 1

尝试XWebView而不复制文件。它为您提供了一个小型 http 服务器。你只需要调用[webview loadFileURL:allowingReadAccessToURL:]


归档时间:

查看次数:

3955 次

最近记录:

11 年,4 月 前