Flutter url_launcher open Facebook link inside app(Facebook) installed but in IOS it just open facebook and not the link
Asked Answered
S

1

5

I have used Url_launcher package;I want to open Facebook link in facebook app if app installed else the browser.This nicely works in Android but in IOS it only open the Facebook app not the link.

The code is :

String digital_url= "https://facebook.com/AliForDigitalIsrael/";

    new ListTile(
                            leading: new SvgPicture.asset(
                              'assets/images/ic_menu_fb.svg',
                              height: 24.0,
                              width: 24.0,
                              color: Colors.black54,
                            ),
                            title: new Text(
                              Strings.fbdigital,
                              textDirection: TextDirection.rtl,
                            ),
                            onTap: () async {
                              var fbUrl =
                                  "fb://facewebmodal/f?href=" + Strings.digital_url;
                              launchFacebook(fbUrl, Strings.digital_url);
                              hideDrawer();
                            },
    
                          ),
         Future<void> launchFacebook(String fbUrl,String fbWebUrl)
      async {
        try {
          bool launched = await launch(fbUrl, forceSafariVC: false);
          print("Launched Native app $launched");
    
          if (!launched) {
            await launch(fbWebUrl, forceSafariVC: false);
            print("Launched browser $launched");
          }
        } catch (e) {
          await launch(fbWebUrl, forceSafariVC: false);
          print("Inside catch");
        }
      }
Spoon answered 8/7, 2020 at 9:18 Comment(4)
Did you edit the info.plist file for iOS ? You have to set the URL Schemes for iOS in order to have deep-link working.Reviviscence
@Reviviscence no i haven't done that,can you share me any link regrading that.Spoon
medium.com/wolox/…Reviviscence
@Reviviscence i don't understand it.Can you please have other link or solution.Spoon
S
11

My working function:

void _launchSocial(String url, String fallbackUrl) async {
  // Don't use canLaunch because of fbProtocolUrl (fb://)
  try {
    bool launched =
        await launch(url, forceSafariVC: false, forceWebView: false);
    if (!launched) {
      await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
      }
  } catch (e) {
    await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
  }
}

Then in onPressed or onTab:

_launchSocial('fb://profile/408834569303957', 'https://www.facebook.com/dorockxl')

Don't forget to add fbYOURPAGEID to CFBundleURLSchemes array in Info.plist:

Note: You can use https://lookup-id.com to find your page's id

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb408834569303957</string>
        </array>
    </dict>
</array>

Btw you can use this method for Twitter and Instagram without an additional setting. Url is enough to open these native apps:

_launchSocial('https://www.instagram.com/YOURNAME/', '')

_launchSocial('https://twitter.com/YOURNAME', '')

Sassafras answered 2/8, 2020 at 3:0 Comment(2)
Which plugin you are using to launch url? because the above code throwing me an error. "Error: Method not found: 'launch'."Underact
I used url_launcher: ^5.5.0 on Flutter 1.20.1. Flutter is new so versions can have big differences.Sassafras

© 2022 - 2024 — McMap. All rights reserved.