I've been working on a hybrid app and I'm looking for a way to transfer data from the WebView to the native application.
My current approach involves using URL variables, and then setting up a listener in Swift to receive the new data.
So far, I've managed to extract the URL variable from the WebView when it initially loads using the following Swift code...
func webViewDidFinishLoad(_ webView: UIWebView) {
print(getQueryStringParameter(url: (webView.request?.url?.absoluteString)!, param: "id"));
}
func getQueryStringParameter(url: String, param: String) -> String? {
guard let url = URLComponents(string: url) else { return nil }
return url.queryItems?.first(where: { $0.name == param })?.value
}
While this method is effective, I haven't been able to find a WebView listener that can detect when a new URL variable is added through JavaScript at a later point after the page has loaded. Here's the JS code I'm using...
window.history.replaceState(null, null, "?id=mydata");
Do you have any suggestions on how I can listen for this change in Swift? Or perhaps there's a better alternative for passing data from a web view to a native app?