akf
akf

Reputation: 41

Flutter InAppWebView - opening other website addresses in the browser

I achieved this thanks to the code below while using Webview

            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://google.com/')) {
                print('allowing navigation to $request');
                return NavigationDecision.navigate;
              } else  {
                print('Opening Default Browser');
                launchURL(request.url); // to open browser 
                return NavigationDecision.prevent;
              }
            },

But when I used inappwebview, the above code didn't work. What should I do to open external web addresses in the browser when using Inappwebview?

InAppWebView: https://pub.dev/packages/flutter_inappwebview

Upvotes: 4

Views: 10206

Answers (1)

Akif Akkaya
Akif Akkaya

Reputation: 301

You can use shouldOverrideUrlLoading:() for InAppWebView.

            shouldOverrideUrlLoading: (controller, shouldOverrideUrlLoadingRequest) async {
              var url = shouldOverrideUrlLoadingRequest.url;
              var uri = Uri.parse(url);



              if ((uri.toString()).startsWith('https://google.com')) {
                return ShouldOverrideUrlLoadingAction.ALLOW;
              }else {
                launchURL(uri.toString());
                return ShouldOverrideUrlLoadingAction.CANCEL;
              }
            },

Upvotes: 7

Related Questions