观察WKWebview的backForwardList变化

发布于 2025-01-16 05:46:07 字数 904 浏览 5 评论 0原文

为了解释我的问题,我想从我的项目的基础知识开始。

我们正在构建一个应用程序,它采用 Web 视图,加载许多不同的网站(Web 应用程序)来形成我们的应用程序包。这些网络应用程序是使用不同的网络技术制作的,例如 ruby​​ 或 ember/react(单页网络应用程序)。稍后通过 push state 更改 url,这对于 webview WKNavigationDelegate 来说是有问题的,因为它无法识别任何这些 url 更改。如果您加载正常的 http 请求(如旧的 Rails 页面),那么一切都很好。

为了随时了解用户在哪个页面,我创建了一个用户脚本,该脚本利用 pushsstate js 原型 并将 url 更改发回 iOS 应用程序,尽管这是一个解决方案在我眼里难以置信。因此,我一直在寻找替代方案,并且发现了 WKWebView.backForwardList 它实际上记录了所有这些推送状态更改。

我现在遇到的问题是如何监视/观察 backForwardListcurrentItem 的变化?您不能使用 KVO 来执行此操作,因为这些属性不支持它。

不过,我确实通过观察 webview.scrollView.contentSize 找到了可能的解决方案,由于某种原因,每次屏幕上发生更改时都会触发该解决方案。奇怪的是,这个观察者会为屏幕上运行的每个动画而被触发,它几乎就像调用滚动视图的像素变化一样。我们的网络应用程序在使用画布元素构建游戏时始终处于动画状态,这意味着观察者被调用很多,并且一直运行它会感到不舒服。

您是否知道一种更好/更简洁的方法来跟踪更改 WKWebView.backForwardList 项目。

干杯托马斯

To explain my problem I would like to start with the fundamentals of my project.

We are building an app which employs a web view which loads a lot of different websites (web apps) to form our app package. Those web apps are made with different web tech like ruby or ember/react (single page web app). The later change urls via push state which is problematic for the webviews WKNavigationDelegate as it doesn't recognise any of those url changes. If you load normal http requests (like old rails pages) then everything is fine and well.

In order to know on which page the user is at any time I created a user script which tapped into the pushsstate js prototype and messaged back the url change to the iOS app and even though this is A solution its in my eyes incredibly hacky. So I have been looking for alternatives and I came across the WKWebView.backForwardList which actually records all those push state changes.

The issue I have now is how do I monitor/observe the backForwardList for changes in lets say the currentItem? You can't use KVO to do so as these properties don't support it.

I did however found a possible solution by observing the webview.scrollView.contentSize which for some reason will trigger every time something changes on the screen. It's odd that this observer is fired for every single animation which is running on the screen its almost as if its called on pixel changes of the scrollview. Our web apps are animating all the time as they build with canvas elements for games which means the observer is called a lot and don't feel comfortable to have this running all the time.

Do you know a nicer/neater way to keep track of the changes WKWebView.backForwardList items.

Cheers Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我找到了看起来很明显但当时不是的解决方案

// Add observation.
urlObservation = webView?.observe(\.url, changeHandler: { (webView, change) in
      print("Web view URL changed to \(webView.backForwardList.currentItem?.url.absoluteString ?? "Empty")", webView.scrollView.contentSize.width, webView.scrollView.contentSize.height)
})

这似乎跟踪了 url 更改,即使导航委托没有以任何方式触发。

I found the solution which seems obvious but wasn't at the time

// Add observation.
urlObservation = webView?.observe(\.url, changeHandler: { (webView, change) in
      print("Web view URL changed to \(webView.backForwardList.currentItem?.url.absoluteString ?? "Empty")", webView.scrollView.contentSize.width, webView.scrollView.contentSize.height)
})

This seems to track the url changes even though the navigation delegate are NOT triggered in any way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文