In my app i have stored Facebook url's. I want to open them in facebook app not in browser. I tried using flutter_url_launcher package but it open the link in default browser ... What i want is to open the link directly into the facebook app . Can anyone suggest any solution or package to help me out of this situation?
The URL must be in this form: “fb://page/page_id” where page_id is the numeric id of the Facebook page. “fb://profile/profile_id” where profile_id is the numeric id of the Facebook profile.
The Facebook app opens links in its built-in browser by default. That is so because it's convenient and just works without burdening you with extra configuration steps. However, not everyone wishes to use Facebook's mobile browser.
I also used the solution from the link @ruelluna mentioned.
Per current version, the below code worked for me:
String fbProtocolUrl;
if (Platform.isIOS) {
fbProtocolUrl = 'fb://profile/page_id';
} else {
fbProtocolUrl = 'fb://page/page_id';
}
String fallbackUrl = 'https://www.facebook.com/page_name';
try {
bool launched = await launch(fbProtocolUrl, forceSafariVC: false);
if (!launched) {
await launch(fallbackUrl, forceSafariVC: false);
}
} catch (e) {
await launch(fallbackUrl, forceSafariVC: false);
}
Also, make sure Info.plist file contains the following:
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[your_page_id]</string>
</array>
</dict>
</array>
You can take your_page_id from: https://findmyfbid.com/
My working function:
void launchUrl2() async{
var url = 'fb://facewebmodal/f?href=https://www.facebook.com/al.mamun.me12';
if (await canLaunch(url)) {
await launch( url, universalLinksOnly: true, );
} else { throw 'There was a problem to open the url: $url'; }
}
I struggled with this for a couple hours and found an updated solution.
First of all, if you add
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[your_page_id]</string>
</array>
</dict>
</array>
To your Info.plist
file as stated by @Al Manum, method canOpenUrl
will always return true, even if Facebook app is not installed.
+1 to this answer for giving me the hint.
Add the following instead:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
</array>
The following code will open Facebook native app if installed on device, else it will open the browser
Future<void> _openFacebook() async {
String fbProtocolUrl;
if (Platform.isIOS) {
fbProtocolUrl = 'fb://profile/{your-page-id}';
} else {
fbProtocolUrl = 'fb://page/{your-page-id}';
}
String fallbackUrl = 'https://www.facebook.com/{your-page-uri}';
try {
Uri fbBundleUri = Uri.parse(fbProtocolUrl);
var canLaunchNatively = await canLaunchUrl(fbBundleUri);
if (canLaunchNatively) {
launchUrl(fbBundleUri);
} else {
await launchUrl(Uri.parse(fallbackUrl),
mode: LaunchMode.externalApplication);
}
} catch (e, st) {
// Handle this as you prefer
}
}
Tested on iOS 15.5, Android 11 and Flutter 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With