Flutter how to open pdf using web browser - flutter

How to open pdf from url address in flutter?
I try call window like this:
And select there application for open pdf file.
I try do it using libraries:
https://pub.dev/packages/pdfx
https://pub.dev/packages/url_launcher
But none of them cannot do it. Any ideas or samples?

You can force webview using
Future<void> _launchInBrowser(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.externalApplication,
)) {
throw 'Could not launch $url';
}
}
or to load it in an inapp web view
Future<void> _launchInWebViewOrVC(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.inAppWebView,
webViewConfiguration: const WebViewConfiguration(
headers: <String, String>{'my_header_key': 'my_header_value'}),
)) {
throw 'Could not launch $url';
}
}

Related

Flutter || url_launcher failed to open specified url

I'm trying to launch this URL ("https://m.me/nagadhat"). but cannot launch on my app. But I can visit this URL from my browser and anywhere else.
String chatURL = "https://m.me/nagadhat";
var url = Uri.parse(chatURL);
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
throw 'Could not launch $url';
}
This is the screenshot when I'm triggering the code
Adding LaunchMode to launchUrl solved my issue.
This is the changed code.
String chatURL = "https://m.me/nagadhat";
var url = Uri.parse(chatURL);
if (await canLaunchUrl(url)) {
await launchUrl(url, mode: LaunchMode.externalApplication);
} else {
throw 'Could not launch $url';
}

How to close the Webview at some point in Flutter

I'm using url_launcher to show the webview inside the app to do the payment, After completion of the payment I have to navigate back to the previous screen. Is there any possibility to do this? Should it be handled from webview or mobile app?
This is the way that im using to show webview
_launchURL(onboardingLink) async {
await canLaunch(onboardingLink)?
await launch(onboardingLink,
enableJavaScript: true,
forceWebView: true)
: throw 'Could not launch $onboardingLink';
}
USE THIS
_onUrlChanged =
flutterWebviewPlugin.onUrlChanged.listen((String url) async {
if (mounted) {
//print("URL changed: $url");
if (url.startsWith(paymentSuccessUrl)) {
// handle success case
} else if (if (paymentCancelUrl)) {
// Handle cancel case
}
}

How to open installed app with url_launcher else open in browser?

like the title suggests, I use url_launcher to open some websites when the user taps a selected icon. The issue is that it opens the browser website if the associated app is not installed on the device otherwise nothing happens if said app is installed. The app just doesn't respond. From what I've read, its supposed to open the associated app anyway? Or am I mistaken?
Here's the onTap:
GestureDetector(
onTap: _launchTwitchURL,
child: Image.asset(
'assets/images/icon_twitch.png', // On click should redirect to an URL
width: 40.0,
height: 40.0,
),
),
And here's the call:
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Using android-intent package like #iDecode suggested to get it to work. appavailability might be a better suited/cleaner package depending on your needs.
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else if (Platform.isAndroid) {
final AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: 'https://www.twitch.tv/example', // replace com.example.app with your applicationId
);
await intent.launch();
} else {
throw 'Could not launch $url';
}
}
Thanks #iDecode

How to launch other android apps using the flutter app

I have used the following method as specified in the documentation.
Future<void> launchUniversalLink(String url) async {
if (await canLaunch(url)) {
final bool nativeAppLaunchSuccess = await launch(url, forceSafariVC: false, universalLinksOnly: true);
print(nativeAppLaunchSuccess);
}else {
print('launch not successfull');
}
}
if I give URL = 'https://www.WhatsApp.com'
print(nativeAppLaunchSuccess); output ==> true
but still the app launches in the browser.
can anyone help me with this problem
Oh I'm sorry. This is my mistake.
Please use 'device_apps' flutter package and usage is below.
And here is a how to know app package name.
https://www.techmesto.com/find-android-app-package-name/
In ios, you know other app's custom Url schema that officially opened.
But usually we can not know that url.
So below ios code is executed, it will open appstore page and need to push 'open' button.
if (Platform.isAndroid) {
if (await DeviceApps.isAppInstalled('com.nbt.moves') ==
true) {
DeviceApps.openApp('com.nbt.moves');
}
} else {
const url =
'https://apps.apple.com/kr/app/%EC%BA%90%EC%8B%9C%EC%8A%AC%EB%9D%BC%EC%9D%B4%EB%93%9C-%EC%8A%A4%ED%85%9D%EC%97%85/id1400703652?uo=4';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}

Url launcher exception in iOS when subject OR body is in Chinese language

Below code working in android device but in iOS device it's throw an exception
dynamic _sendEmail() async {
String url = 'mailto:support#test.com?subject=请写以上这个线';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Could not launch mailto:support#k8sllc.com?subject=请写以上这个线
You should encode your url:
String url = Uri.encodeComponent('mailto:support#test.com?subject=请写以上这个线');
so it looks like this:
mailto%3Asupport%40test.com%3Fsubject%3D%E8%AF%B7%E5%86%99%E4%BB%A5%E4%B8%8A%E8%BF%99%E4%B8%AA%E7%BA%BF