在 webView 上加载本地存储的文件时出现延迟 [英] Delay while loading locally stored file on webView

查看:26
本文介绍了在 webView 上加载本地存储的文件时出现延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 webView 上加载本地存储的 html(带有 javascript 的文件)文件.这些文件加载​​在我的 ViewController.swift 中.但是,第一次启动应用程序时会有大约 2-3 秒的延迟.我在网上搜索了解决此问题的方法,根据我观察到的方法(webView 的首次加载延迟了资源的加载),我决定使用 NSNotificationCenter 在我的 ViewController 中,并从AppDelegate"调用它们,以便第一次加载 webView.

I am loading locally stored html(files with javascript) file on my webView. These files are loaded in my ViewController.swift. However, there is a delay of ~2-3 seconds when the app is launched for the first time. I have searched over the web regarding an approach to fix this issue, with the approaches I observed(First loading of the webView delays the loading of resources), I have decided to use NSNotificationCenter in my ViewController and call them from 'AppDelegate' so that the webView is loaded first time.

在我的代码中实施这些更改后,应用程序的 LaunchScreen 出现延迟,这与我在此处加载文件时预期的一样,但是 的加载时间ViewController 还是一样.

Upon implementation of these changes in my code, there is delay in the LaunchScreen of the application, which is as expected as I am loading a file here, however, the loading time of ViewController is still the same.

我正在调用通知AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        NSNotificationCenter.defaultCenter().postNotificationName("loadDefaultWebViewID",object:nil)


        return true
    }

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadDefaultWebView:", name: "loadDefaultWebViewID", object: nil)
    if let htmlURL = NSBundle.mainBundle().URLForResource("index", withExtension: "html") {
        let requestObject = NSURLRequest(URL: htmlURL)
        webViewHTML5Player.loadRequest(requestObject)

    }

我希望减少 ViewController 上的延迟.如果这不是正确的方法,是否还有其他更好的优化方法来实现此任务?

I wish to reduce the delay that on the ViewController.If this is not the correct way, Is there any other better optimized approached to achieve this task ?

编辑 1

为此目的,我尝试使用 WKWebView(使用 preferences.javaScriptEnabled = true)和 UIWebView,加载时间仍然相同.

I have tried using WKWebView(with preferences.javaScriptEnabled = true) and UIWebView for this purpose, loading time is still the same.

推荐答案

这是 UIWebView 中常见的问题.我的观点是使 web 视图最初隐藏(显示某种正在加载..."指示符),然后在 webViewDidFinishLoad: 方法 中取消隐藏它.你也可以参考这个链接 UIWebView 降低加载时​​间,更多有效的方式预加载 webView 不起作用 -尝试减少加载时间

This is a common problem occuring in UIWebView. My point is to make the web view initially hidden (showing some sort of "Loading..." indicator instead), then make it un-hidden in the webViewDidFinishLoad: method. and also you can refer this link UIWebView lower loading time with a more efficient way and Preloading webView doesn't work - trying to reduce loading time

这篇关于在 webView 上加载本地存储的文件时出现延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆