iOS Wrapped PWA: How to get url parameters of webview?

Hy everyone,

I’ve built a single page PWA with and generated the xcode project with PWAbuilder.
I now need xcode to be able to extract a URL parameter from the PWA that is inside webview.
But when running the app on my iPhone, I can see in the xcode console that any change in the URL doesn’t get detected. It only prints the main url once and it doesn’t change when I navigate.

Does anyone know how to fix that?


Hi @snowsnow

It sounds like the issue might be related to the configuration of the webview in the Xcode project. By default, the webview may not be set up to handle navigation events or URL changes.

One possible solution is to add a WKNavigationDelegate to the webview, which will allow you to receive callbacks when the URL changes or the webview navigates to a new page. Here’s an example of how you can do this:

  1. In your view controller that contains the webview, add WKNavigationDelegate to the list of protocols it conforms to:

class ViewController: UIViewController, WKNavigationDelegate {
// …

  1. Set the webview’s navigationDelegate property to be the view controller:

webView.navigationDelegate = self

  1. Implement the webView(_:didStartProvisionalNavigation:) method in the view controller. This method will be called when the webview starts to load a new page:

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
if let url = webView.url {
print(“URL changed: (url)”)

  1. Run the app and test the navigation. The didStartProvisionalNavigation method should now be called every time the webview navigates to a new page, and you can extract the URL parameter from the url property of the webview.

Note that you may also need to handle other methods of the WKNavigationDelegate protocol, such as webView(_:didFail:withError:) and webView(_:didFinish:), to handle errors or completion of the navigation.

1 Like

thanks for this @jonathan.hernandez !

You are welcome @snowsnow

This topic was automatically closed after 70 days. New replies are no longer allowed.