I use flutter_webview_plugin and I would like to open a link; not in my webview, but in the browser of the user. When I use StreamSubscription with String, the app listens only to the url from the app, like when I use FlutterWebViewPlugin().reloadUrl or .launchUrl.
You should be able to use the url_launcher package concurrently with the flutter_webview_plugin to get your desired effect.
Basically with the webview, you can use the parameter invalidUrlRegex in either launch or the WebviewScaffold to setup URLs not allowed to be opened inside your webview.
eg: below blocks any links that dont have the host google or stackoverflow.
invalidUrlRegex: r'^(?!https:\/{2}www\.google\.com|https:\/{2}stackoverflow\.com).*$'
Then have an onStateChanged listener on the webview, this will cause any blocked URLs to result in a WebViewState.abortLoad state.
FlutterWebviewPlugin().onStateChanged.listen(onStateChanged);
onStateChanged(WebViewStateChanged change) {
if (change.type == WebViewState.abortLoad) {
canLaunch(change.url).then((val) => val ? launch(change.url) : null);
}
}
The above will use the url_launcher to launch the URL in the user's preferred browser.
This is by far the best approach that I was able to come up within our application.
Related
I just released my first app and It has a button in it that takes you to a website.
A user just sent me this:.
I tried googling Google's secure browsers policy but not much info is coming up.
how can I make my app comply with this policy? I think the button opens a browser in app (I use duckduckgo as my default browser and haven't had an issue)
is it just a case of opening a browser and then heading to the website when the button is pressed?
my code to open the website is:
_launchURL() async {
const url = 'https://www.thiswebsite.com';
final uri = Uri.parse(url);
if (await canLaunchUrl(uri)) {
await launchUrl(uri);
} else {
throw 'Could not launch $url';
}
}
thanks so much and any help would be greatly appreciated
Google is trying to make sure, you open this window in an actual new browser window, not in a webview still under the control of your application.
Your code should open an external browser.
Maybe the user has no browser installed on their device? Maybe their default browser is some exotic thing not recognized by Google?
If you are using the latest version of url_launcher (currently 6.1.8) there is not a lot more you can do.
You could force the app to take the external browser, not the in-app webview:
await launchUrl(_url,mode: LaunchMode.externalApplication);
But that should be what happens anyway. If your version is up to date, ask your user, what browser they use. Be prepared to tell them that they need to use another one.
I want to open Google calendar app more specifically the create event page in the app from my another flutter app.
I am using the URL launcher package but it opens the app in chrome
What change should I make in the URL so that the add event page opens directly in the google calendar app.
Currently my URL looks like below
https://calendar.google.com/calendar/u/0/r/eventedit?dates=20210226T033000/20210226T040000&ctz=Asia/Calcutta&location&text=Blawsome:+A+Crystal+Alchemy+Healing+Meditation&details=Parth+Pitroda
My code for that part is as below
if (await canLaunchUrl(Uri.parse('https://calendar.google.com/calendar/u/0/r/eventedit?dates=20210226T033000/20210226T040000&ctz=Asia/Calcutta&location&text=Blawsome:+A+Crystal+Alchemy+Healing+Meditation&details=Parth+Pitroda'))) {
await launchUrl(
Uri.parse('https://calendar.google.com/calendar/u/0/r/eventedit?dates=20210226T033000/20210226T040000&ctz=Asia/Calcutta&location&text=Blawsome:+A+Crystal+Alchemy+Healing+Meditation&details=Parth+Pitroda'),
mode: LaunchMode.externalApplication);
} else {
throw 'Could not launch URL';
}
rathe than using a HTTP request, you could use googleapis package from https://pub.dev which has inbuilt methods to create Google Calender events directly within the Calender app.
Check package and documentation here.
Happy coding!
I am getting dynamic link in notification and I want to open dynamic link on notification click.
I am using url_launcher for this purpose but it takes me first to default browser and process the link there and take me back to app.
Is it possible we can process the link inside the app only. I have tried Webview but webview is not opening with dynamic link.
As far as I understand you want to read data from uri link which is generated by firebase in that situation, below code should work :
PendingDynamicLinkData? data =
await FirebaseDynamicLinks.instance.getDynamicLink("Your url here");
String? strLinkData = data.link.toString();
Once you get strLinkData you can move further and do your action.
you can send link like below image
I have a requirement ,in app I need to navigate the user to webview (website) and there will be a form with 5 fields and once the form is submitted a flag to be passed to app and app should work based on the flag .. So how it can be achieved
I have checked that there is a url_launcher or webview_flutter but i don know how to redirect the app once the form is submitted in website
I am not entirely sure if I understood the problem. But here is what can be done.
Open url in webview.
Now you should listen to webview state change. There must be a listener for this. say the listener is webViewStateChanged which will give you the state of webview.
Then you can check like
void webViewStateChanged(WebViewStateChanged newState) async {
if (newState.type != WebViewState.finishLoad ||
newState.url != desiredURL) {
return;
}
// At this point, you want to return to your app. If you need data from
// website, you can do so by accessing cookies of webview.
// Now you are back to the app, you can pop/replace the webview whatever
// is your requirement.
I am using flutter_webview_plugin: ^0.3.11
This is my code
Widget build(BuildContext context){
return WebviewScaffold(
url: glbPhotoURL,
withJavascript: true,
scrollBar : true,
withZoom: true
)
url: glbPhotoURL => here glbPhotoURL is a URL that I am passing
When I am using any normal URL it is running fine (like http://www.google.com, http://youtube.com"
Even url like - https://youtu.be/o5UPfG1eIw4 is running fine
But when I am using any google photo url (short url) it is throwing an error net::ERR_UNKNOWN_URL_SCHEME for eg - https://photos.app.goo.gl/FkQenAD8kQQc4TSr6
If I am using the expanded URL it shows the pictures -https://photos.google.com/share/AF1QipNItZG3Cg_hn9__2QnuVh3nNMbRuGxQaQSWZ76qni7L7h0ORbauolcH3AKe0MOnEA?
key=emc1Mk1CenRJRjloMjV5V1AzcmczNUprcGFsbmR3
Please help me resolve the issue
As of now I am running it on Android physical device.
Google Photos uses Firebase Dynamic Links. I suggest launching the link externally. I encountered a similar error on Android before, when Firebase Dynamic Links are being forced to be loaded in a WebView. FDLs are expected to be handled by Google Play Services in Android. But since the WebView doesn't know what to do with the link it's forced to display, the WebView returns "net::ERR_UNKNOWN_URL_SCHEME" error.
Open the link externally by using url_launcher. Use RegEx to filter intent URLs and check if the URL can be launched and be handled externally (outside the app).
var yourURL = "URL goes here";
// Check if URL contains Google Photos URL
yourURL.contains(RegExp('^https://photos\.app\.goo\.gl/.*$')){
// Check if the URL can be launched
if (await canLaunch(yourURL)) {
await launch(yourURL);
} else {
print('Could not launch $yourURL');
}
}