How to launch other android apps using the flutter app - flutter

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

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

Flutter and Whatsapp Group

I managed to send whatsapp messages from my app with
launchWhatsapp(String num) async {
String url;
if (Platform.isAndroid) {
url = "https://wa.me/$num/?text=${Uri.parse('test')}"; // new line
} else {
url =
"https://api.whatsapp.com/send?phone=$num=${Uri.parse('test')}"; // new line
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
but I need to send the same message to multiple numbers, so how can I achieve this?
I can't use a loop because in that case the user should anyway confirm manually one by one so it would become very annoying.
I was thinking about creating a whatsapp group, but apparently that's not possible from flutter. Or maybe that's not the correct solution.
Any idea?

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 create a method that will call URL's From a list with flutter

I am working with google books in my flutter project.
im trying to create a method that that will call the diffrent books URL's that i have stored in a List and call the list back using a future builder.
The list look somthing like this:
List<String> toBeReadBooksList = ['https://www.googleapis.com/books/v1/volumes?q=1086782593+isbn'];
You can do it a way that similar to this:
final url = 'https://www.googleapis.com/books/v1/volumes?q=1086782593+isbn';
final response = await http.get(url)
if (response.statusCode == 200) {
// Here you need to parse data from response, you should replace it with your realization:
return Data.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load data from $url');
}
You can read more about how to work with requests and responses to/from the Network in Official Documentation.
Import url_launcher plugin import 'package:url_launcher/url_launcher.dart';
and in your pubspec.yaml dependecies url_launcher: ^5.7.10
add this and call it to open the selected url
_launchURL() async {
const url = toBeReadBooksList[0];//your list url
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