gpt4 book ai didi

Flutter 用短信打开 whatsapp

转载 作者:行者123 更新时间:2023-12-03 20:26:07 25 4
gpt4 key购买 nike

我想从我的 Flutter 应用程序打开 whatsapp 并发送一个特定的文本字符串。当我在whatsapp中时,我会选择将其发送给谁。
在做了一些研究之后,我想出了这个:

_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';
}
}
哪个工作正常,但是有两个问题:
  • 一旦我将文本字符串变成多个单词,它就会失败。所以如果我把它改成:

  • _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。
  • 我的手机上已经安装了 whatsapp,但它没有直接进入应用程序,而是先给我一个网页和打开应用程序的选项。

  • 这是我看到的网页:
    enter image description here
    任何解决这些问题的任何帮助将不胜感激。
    谢谢
    卡森
    附言我正在使用 Url_launcher 包来做到这一点。

    最佳答案

    来自 official Whatsapp FAQ ,您可以看到使用“通用链接是链接到 WhatsApp 帐户的首选方法”。
    所以在你的代码中,url 字符串应该是:

    const url = "https://wa.me/?text=YourTextHere";
    如果用户在手机中安装了Whatsapp,此链接将直接打开它。那应该先解决打开网页的问题。
    对于无法发送多字消息的问题,那是因为您需要将消息编码为 URL。这在文档中也有说明:

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


    因此,为了在 Dart 中对您的消息进行 url 编码,您可以按如下方式进行:
    const url = "https://wa.me/?text=Your Message here";
    var encoded = Uri.encodeFull(url);
    正如在 Dart Language tour 中看到的那样.
    请注意,在您的示例代码中,您在文本消息周围添加了一组额外的单引号,您不应该这样做。
    编辑:
    Whatsapp FAQ 中还提供的另一个选项是直接使用 Whatsapp Scheme。如果您想尝试,可以使用以下网址:
    const url = "whatsapp://send?text=Hello World!"
    另请注意,如果您在 iOS9 或更高版本中进行测试, Apple Documentation状态:

    Important

    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.


    因此,您需要将以下键添加到 info.plist 中,以防您使用自定义 whatsapp 方案:
    <key>LSApplicationQueriesSchemes</key>
    <array>
    <string>whatsapp</string>
    </array>

    关于Flutter 用短信打开 whatsapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60947937/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com