2

I am trying to open apps like WhatsApp, Twitter and Facebook from inside my flutter app but I get a URL scheme error. Please help! The code I am using is

                      child: ElevatedButton(
                        onPressed: () async {
                          String url =
                              "https://api.whatsapp.com/send?text=Hello there!";
                          var encoded = Uri.encodeFull(url);
                          Uri whatsAppUri = Uri.parse(encoded);
                          if (await canLaunchUrl(whatsAppUri)) {
                            await launchUrl(whatsAppUri);
                          }
                        },

I get the following error screen- Error screen when button is pressed

Please help me find the correct method and URL. Also please help me with Twitter and Facebook as I need to use them as well. Thank You!

ayush
  • 464
  • 5
  • 17

2 Answers2

1

You can use below code:

String url() {
    if (Platform.isAndroid) {
      return "https://wa.me/$phone/?text=Hello there!";
    } else {
      return "https://api.whatsapp.com/send?phone=$phone=Hello there!}";
    }
  }

String url = url();
var encoded = Uri.encodeFull(url);
                          Uri whatsAppUri = Uri.parse(encoded);
                          if (await canLaunchUrl(whatsAppUri)) {
                            await launchUrl(whatsAppUri);
                          }

In the place of $phone you can write phone number of user to which you want to send.

Rohan Jariwala
  • 1,564
  • 2
  • 7
  • 24
0

Use this:

const url = "whatsapp://send?text=Hello World!"

or

const url = "https://wa.me/?text=Hello World!";

and add this to your info.plist if your using ios:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

for facebook:

const url = "fb://facewebmodal/f?href=$username";

for twitter:

const url = "https://twitter.com/username";

last but not least make sure you add configuration right like:

<intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
</intent>

<intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
</intent>

you can find more information from here

eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
  • 1
    For Android, I cannot open WhatsApp with the URLs mentioned above. Getting message like I/chromium(28471): [INFO:CONSOLE(29)] "Refused to load the font 'https://static.whatsapp.net/rsrc.php/yH/r/c_1vdG88uNh.woff2' because it violates the following Content Security Policy directive: "font-src data: https://*.fbcdn.net". Also getting the same screen as above – ayush Aug 17 '22 at 19:22
  • are adding android configuration that package said? because the second one is opening in my android device. – eamirho3ein Aug 17 '22 at 19:27
  • can you please specify it, package only have for phone and SMS I guess. – ayush Aug 18 '22 at 06:49
  • 1
    I will check with this again. Thank You! – ayush Aug 18 '22 at 08:52