保持 WKWebView 及其 UIViewController 在后台运行并可从多个 ViewController 访问 [英] Keeping a WKWebView and it's UIViewController in the background running and accessible from multiple ViewControllers

查看:25
本文介绍了保持 WKWebView 及其 UIViewController 在后台运行并可从多个 ViewController 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:为了向 API 端点发出 Web 请求,我需要每 25-30 秒抓取一个网站并检索一个令牌.我正在使用 WKWebView 执行此操作,并使用 WKUserScript 以检索包含令牌的 AJAX 响应标头.请特别关注这个问题,而不是这个背景信息 - 我完全是为了我自己的教育目的而尝试这样做.

Background: In order to make web requests to an API endpoint, I need to scrape a website and retrieve a token every 25-30 seconds. I'm doing this with a WKWebView and injecting some custom JavaScript using WKUserScript to retrieve AJAX response headers containing the token. Please focus on the question specifically and not on this background information - I'm attempting this entirely for my own educational purposes.

目标

我将有不同的模型"类,甚至其他 UIViewController,它们可能需要调用共享的 UIViewController 来检索此令牌以发出经过身份验证的请求.

Goal

I will have different 'model' classes, or even just other UIViewControllers, that may need to call the shared UIViewController to retrieve this token to make an authenticated request.

也许我可以将其抽象为一个Sdk"类.无论如何,这个模型"SDK 类可以被任何其他ViewController 实例化和使用.

Maybe I might abstract this into one "Sdk" class. Regardless, this 'model' SDK class could be instantiated and used by any other ViewController.

我希望能够调用 WKWebViewUIViewController 并检索一些数据.除非我每 25 秒重新创建一次,否则我需要在后台运行它或共享它.我希望能够在后台"运行 UIViewController 并在 WKWebView 完成它的事情后从中接收一些信息.

I would like to be able to call the UIViewController of the WKWebView and retrieve some data. Unless I re-create it every 25 seconds, I need to run it in the background or share it. I would like to be able to run a UIViewController 'in the background' and receive some information from it once WKWebView has done it's thing.

我知道有多种方式可以与另一个 ViewController 通信,包括委托和转场.但是,我不确定这些是否能帮助我保持包含 WKWebView 的视图存在于后台,以便我可以将其称为 ViewController 并让它重新执行抓取.委托可能适用于普通代码,但是对于必须存在视图的代码呢?每次不同的模型或视图控制器尝试获取此令牌时,我是否都必须动态地重新创建此 WKWebView?

I know there are multiple ways of communicating with another ViewController including delegation and segueing. However, I'm not sure that these help me keep the view containing the WKWebView existing in the background so I can call it's ViewController and have it re-perform the scrape. Delegation may work for normal code, but what about one that must have the view existing? Would I have to re-create this WKWebView dynamically each time a different model, or view controller, were to try and get this token?

一篇文章建议使用ContainerViewControllers.由此,我在主"ViewController(包含其他视图的那个)中收集到,我可以放置隐藏的 WKWebView 来做它的事情并与孩子交流以这种方式通过委托查看控制器.

One post suggests utilising ContainerViewControllers. From this, I gather that in the 'master' ViewController (the one containing the other ones), I could place the hidden WKWebView to do it's thing and communicate to the child view controllers that way via delegation.

另一篇文章建议使用 AppDelegate 并将其设为共享服务.我完全反对使用 Singleton,因为它被广泛认为是一种反模式.必须有另一种方法,即使稍微复杂一点,也可以帮助我做我想做的事,而无需诉诸这种欺骗".

Another post suggests using AppDelegate and making it a shared service. I'm completely against using a Singleton as it is widely considered an anti-pattern. There must be another way, even if a little more complex, that helps me do what I want without resorting to this 'cheat'.

这篇文章讨论了多个ViewControllers之间的通信,但我不知道当某些东西需要保持运行和执行时,这会有什么用处.

This post talks about communicating between multiple ViewControllers, but I can't figure out how this would be useful when something needs to stay running and executing things.

还有其他方法可以做到这一点吗?用强指针在后台线程中运行一些东西,这样它就不会被丢弃?我使用的是 Xcode 9.2、Swift 4 和 iOS 11.由于我对 iOS 编程非常陌生,因此将不胜感激任何关于此的小代码示例.

How about any other ways to do this? Run something in a background thread with a strong pointer so it doesn't get discarded? I'm using Xcode 9.2, Swift 4, and iOS 11. As I'm very new to iOS programming, any small code examples on this would be appreciated.

推荐答案

不幸的是,WKWebView 必须在视图层次结构中才能使用它.您必须将其添加为屏幕视图控制器的子视图.

Unfortunately, WKWebView must be in the view hierarchy to use it. You must have added it as a sub view of an on-screen view controller.

这对我来说很好.我在屏幕外添加了这个,所以它不可见.隐藏属性也可能有效.无论哪种方式,您都必须使用它调用 addSubview 以使其工作.

This was fine for me. I added this off-screen so it was not visible. Hidden attribute might have worked as well. Either way you must call addSubview with it to make it work.

还有一些其他的问题并在此处回答验证这一点.

这篇关于保持 WKWebView 及其 UIViewController 在后台运行并可从多个 ViewController 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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