Reputation: 41
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
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