Flutter || url_launcher failed to open specified url - flutter

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';
}

Related

Flutter how to open pdf using web browser

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';
}
}

Launch link in flutter

i would like to launch custom dynamic links..
i have getter which load string text from database, this string text is user input with them IG username
bio is IG username, exsample #instagram
String getBio(String bio) {
if (widget.isMyProfile) {
return bio;
} else if (bio == "") {
return "";
} else {
return bio;
}
}
i want to have static link "**https://instagram.com/**;" where end of link will be dynamic instagram user name
example: 'https://instagram.com/instagram';
_launchURLIG() async {
const url = 'https://instagram.com/';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
=====
something like this. https://instagram.com' + getBio
_launchURLIG() async {
const url = 'https://instagram.com' + getBio ;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
You should past profile as a parameter to your function
_launchURLIG(String userId) async {
String url = 'https://instagram.com/' + userId ;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
Maybe you need this package to run link:
https://pub.dev/packages/url_launcher

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

How do you catch canLaunch exceptions using url_launcher in flutter?

Using the code from the package I was unable to catch the exception. Note that I would like to catch this specific exception.
// from https://pub.dev/packages/url_launcher
_launchURL() async {
const url = 'myscheme://myurl';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
// my code
try {
_launchURL();
}
catch (e)
{
// although the exception occurs, this never happens, and I would rather catch the exact canLaunch exception
}
I would try to put the try catch statement inside the function. I believe what is happening is that the try/catch statement is only applying for the function call and although it is async I dont believe that it actually tries and returns exeptions.
So the solution would look somethink like this:
_launchURL() async {
try{
const url = 'myscheme://myurl';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
catch(e){
//debug e
}
}
// my code
launchURL();
You can use .then() for business logic.
For me, it is used to check if the app can be opened on the device.
Can be solution below,
--> url_launcher: ^6.0.2
--> https://pub.dev/packages/url_launcher
launch(appLink).then(
(bool isLaunch) {
print('isLaunch: $isLaunch');
if (isLaunch) {
// Launch Success
} else {
// Launch Fail
}
},
onError: (e) {
print('onError: $e');
},
).catchError(
(ex) => print('catchError: $ex'),
);
Work for me.
Future<void> _launch(String url) async {
await canLaunch(url)
? await launch(url)
: throw 'Could not launch $url';
}