Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open deep link in flutter_inappwebview when opening init url and then the user is clicking icon app for opening deep link?

I'm using flutter_inappwebview version 5.4.4+3 and having a problem with deep link on WebView.

InAppWebView(
   initialUrlRequest: URLRequest(url: Uri.parse('https://www.google.com/contact/')),
)

When The InitUrl was opened in InApp WebView of the application, This page has an icon app (example: youtube, linkedIn, etc...) for the user's clicking and navigating to the respective app.

page_init

After the user clicks, My application can't load this page or navigate to the respective app.

Example Deep link: myapp://openapp?type=...

unload_page

How to fix this problem?

like image 796
Huu Bao Nguyen Avatar asked Oct 18 '25 15:10

Huu Bao Nguyen


2 Answers

I had this problem, After googling for a while I found nothing.

The solution that Eugene Kuzmenko was provided is a good solution but when the user goes to another website and the URL is different It's going to be launched in an external browser, To solve this my code lauches the deep link If It doesn't have https or http.

Hope It helps:

  Future<NavigationActionPolicy?> checkDeepLink(
      InAppWebViewController inAppWebViewController,
      NavigationAction navigationAction) async {
    final url = await state.webviewController?.getUrl();
    final deepLink = navigationAction.request.url;

    if (deepLink != null &&
        url != navigationAction.request.url &&
        (deepLink.scheme != 'https' && deepLink.scheme != 'http')) {
      launchUrl(deepLink, mode: LaunchMode.externalNonBrowserApplication);
      return NavigationActionPolicy.CANCEL;
    }

    return NavigationActionPolicy.ALLOW;
  }
like image 90
Reza Aslejeddian Avatar answered Oct 21 '25 13:10

Reza Aslejeddian


I faced up the same problem.

I suppose your app already handles deeplinks and we can skip this step.

In this case, you have to add shouldOverrideUrlLoading to your InAppWebView and open your link with the url_launcher plugin.


InAppWebView(
    shouldOverrideUrlLoading: (controller, navigationAction) async {
    
    final url = await controller.getUrl();
    final deeplink = navigationAction.request.url;

    if (deeplink != null && url != navigationAction.request.url) {
         launchUrl(deeplink, mode: LaunchMode.externalNonBrowserApplication);

         return NavigationActionPolicy.CANCEL;
    }

   return NavigationActionPolicy.ALLOW;
   },
   initialSettings: InAppWebViewSettings(
     useShouldOverrideUrlLoading: true,
 ),
)
....

Alternatively, handle deep links within shouldOverrideUrlLoading. Check for your deep link host, parse the path, and navigate using your preferred router (AutoRoute, GoRouter, etc.).

like image 27
Eugene Kuzmenko Avatar answered Oct 21 '25 13:10

Eugene Kuzmenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!