I want to redirect from my flutter app to social media platform apps using a profile/business URL. I am using the url_launcher dart package. I was able to find the endpoint to redirect to a Facebook profile but don't know how to open specific Instagram and Twitter pages.
_launchFacebook(String? facebook) async {
final url = 'fb://facewebmodal/f?href=https://' + facebook!;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
_launchTwitter(String? twitter) async {
final url = 'tw://' + twitter!;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
_launchInstagram(String? instagram) async {
final url = 'in://' + instagram!;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
Facebook is a bit of an odd case here, asking for a specific protocol fb://facewebmodal/f?href=$url
.
Usually, applications register the URL of their website for deep linking, which means they will open in your browser if you don't have the corresponding app installed, and directly in their app if you do.
This is the implementation I usually use, which definitely works for Facebook, Twitter, Instagram and LinkedIn. I included a special check for Android, so that if you don't have Facebook installed, it will open up the link correctly in the browser instead.
@override
Future<void> launchUrl(String url) async {
final _canLaunch = await canLaunch(url);
if (kIsWeb) {
if (_canLaunch) {
await launch(url);
} else {
throw "Could not launch $url";
}
return;
}
if (TargetPlatform.android) {
if (url.startsWith("https://www.facebook.com/")) {
final url2 = "fb://facewebmodal/f?href=$url";
final intent2 = AndroidIntent(action: "action_view", data: url2);
final canWork = await intent2.canResolveActivity();
if (canWork) return intent2.launch();
}
final intent = AndroidIntent(action: "action_view", data: url);
return intent.launch();
} else {
if (_canLaunch) {
await launch(url, forceSafariVC: false);
} else {
throw "Could not launch $url";
}
}
}
You can without any problem open links in the https://instagram.com/username
and the https://twitter.com/username
formats with it.
Hope it helps !