(Xcode, Swift) 如果它们在不同的 viewControllers 上,是否可以同时加载多个 WKWebViews? [英] (Xcode, Swift) Is it possible to load multiple WKWebViews simultaneously if they are on different viewControllers?

查看:34
本文介绍了(Xcode, Swift) 如果它们在不同的 viewControllers 上,是否可以同时加载多个 WKWebViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个 viewController,它们都有不同的 WKWebView.当用户启动应用程序并看到第一个 viewController 时,我希望其他 viewControllers 的 WKWebViews 在后台加载,以便应用程序在从 viewController 切换到 viewController 时看起来更加无缝.

I have 4 viewControllers that all have different WKWebViews. When the user starts up the app and sees the first viewController, I want the WKWebViews of the other viewControllers to load in the background so that the app will seem more seamless when switching from viewController to viewController.

这可能吗?或者我是否必须将所有 webView 放在一个 viewController 中?

Is this possible? Or would I have to put all the webViews in one viewController?

推荐答案

您试图避免的几乎所有延迟都是由于下载数据;一旦数据可用,实例化 web 视图的速度足够快,用户不会注意到任何延迟.鉴于此,您真正应该做的是下载数据,而不是实例化比您需要的更多的 Web 视图.BJHStudios 在评论中建议的问题为此提供了几个选项,但您应该查看 NSURLSession 而不是 NSURLConnection;NSURLSession 非常易于使用并提供了很大的灵活性.例如,您可以配置会话以在应用处于非活动状态时下载数据.

Nearly all of the delay that you're trying to avoid is due to downloading the data; instantiating a web view once the data is available is fast enough that users won't notice any delay. Given that, what you should really be looking to do is to download the data, not instantiate more web views than you need. The question that BJHStudios suggested in a comment provides a couple options for this, but you should look at NSURLSession rather than NSURLConnection; NSURLSession is very easy to use and offers a lot of flexibility. For example, you can configure a session to download data when the app is inactive.

如果您的应用使用数据模型,您可以将四个视图控制器中的每一个所需的网络资源合并到模型中.当您的应用启动时,它自然会实例化模型,然后模型会检索必要的资源,以便在需要时立即显示它们.

If your app makes use of a data model, you could incorporate the web resources required by each of the four view controllers into the model. When your app starts up, it would naturally instantiate the model, and the model would then retrieve the necessary resources so that they'd be immediately available for display when needed.

这篇关于(Xcode, Swift) 如果它们在不同的 viewControllers 上,是否可以同时加载多个 WKWebViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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