I am using SwiftUI, so I have a wrapper around the Webview, like this:
import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {
var url: String
let webView = WKWebView()
func makeUIView(context: Context) -> WKWebView {
webView.evaluateJavaScript("navigator.userAgent") { (result, error) in
print(result as! String)
}
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let request = URLRequest(url: URL(string:url)!)
uiView.load(request)
}
}
How can I execute a function/method whenever the user makes scroll to the website?
2
Answers
To get scroll position updates you need to create coordinator and override
makeCoordinator()
method and return instance of your coordinator. InmakeUIView(_:)
method just assign scrollview delegate tocontext.coordinator
(context object in provided in arguments of makeUIView method)Pass binding from view to coordinator and coordinator is responsible to update that binding. Here is code for that
WebView with UIScrollViewDelegate support.
Observable ScrollViewDetector
Agreement view with sticky footer button.