颤振用文本消息打开whatsapp

问题描述:

我想从Flutter应用程序中打开whatsapp并发送特定的文本字符串.在whatsapp中,我将选择发送给谁.

I want to open whatsapp from my Flutter application and send a specific text string. I'll select who I send it to when I'm in whatsapp.

进行一些研究后,我想到了这一点:

After making some research I came up with this:

_launchWhatsapp() async {
  const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

哪个工作还可以,但是有两个问题:

Which works ok ish, however there are two problems:

  1. 一旦我将文本字符串转换成多个单词,它就会失败.因此,如果我将其更改为:

_launchWhatsapp() async {
   const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
   if (await canLaunch(url)) {
     await launch(url);
   } else {
   throw 'Could not launch $url';
  }
}

然后抛出无法启动$ url.

Then the Could not launch $url is thrown.

  1. 我的手机上已经安装了whatsapp,但是它并没有直接转到该应用程序,而是为我提供了一个网页以及打开该应用程序的选项.

这是我看到的网页:

对于解决这些问题的任何帮助,将不胜感激.

Any help on resolving either of these issues would be greatly appreciated.

谢谢

卡森

P.s.我正在使用Url_launcher软件包来做到这一点.

P.s. I'm using the Url_launcher package to do this.

来自官方Whatsapp常见问题解答,您会看到使用通用链接是链接到WhatsApp帐户的首选方法".

From the official Whatsapp FAQ, you can see that using "Universal links are the preferred method of linking to a WhatsApp account".

因此,在您的代码中,URL字符串应为:

So in your code, the url string should be:

const url = "https://wa.me/?text=YourTextHere";

如果用户的手机中安装了Whatsapp,则此链接将直接打开它.那应该可以解决先打开网页的问题.

If the user has Whatsapp installed in his phone, this link will open it directly. That should solve the problem of opening a webpage first.

对于无法发送多字消息的问题,这是因为您需要将消息编码为URL.多数民众赞成在文档中也指出:

For the problem of not being able to send multi-word messages, that's because you need to encode your message as a URL. Thats stated in the documentation aswell:

URL编码的文本是URL编码的预填充消息.

URL-encodedtext is the URL-encoded pre-filled message.

因此,为了在Dart中对您的消息进行网址编码,您可以按以下步骤进行操作:

So, in order to url-encode your message in Dart, you can do it as follows:

const url = "https://wa.me/?text=Your Message here";
var encoded = Uri.encodeFull(url);

Dart语言导览中可以看到.

请注意,在示例代码中,您在文本消息周围添加了一组额外的单引号,您不应该这样做.

Please note that in your example-code you have put an extra set of single quotes around the text-message, which you shouldn't.

Whatsapp常见问题解答中还提供了另一个选项,就是直接使用Whatsapp方案.如果您想尝试一下,可以使用以下网址:

Another option also presented in the Whatsapp FAQ is to directly use the Whatsapp Scheme. If you want to try that, you can use the following url:

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

还请注意,如果您正在iOS9或更高版本中进行测试,则 Apple文档指出:

Please also note that if you are testing in iOS9 or greater, the Apple Documentation states:

重要

如果您的应用在iOS 9.0或更高版本上链接,则必须通过将LSApplicationQueriesSchemes密钥添加到应用的Info.plist文件中来声明传递给此方法的URL方案.无论是否安装了适当的应用程序,此方法对于未声明的方案始终返回false.要了解有关密钥的更多信息,请参见LSApplicationQueriesSchemes.

If your app is linked on or after iOS 9.0, you must declare the URL schemes you pass to this method by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. This method always returns false for undeclared schemes, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.

因此,如果您使用的是自定义whatsapp方案,则需要在info.plist中添加以下键:

So you need to add the following keys to your info.plist, in case you are using the custom whatsapp scheme:

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