Kit*_*tcc 5 whatsapp flutter flutter-dependencies
我想从我的 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';
}
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但是有两个问题:
_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';
}
}
Run Code Online (Sandbox Code Playgroud)
然后抛出无法启动 $url。
这是我看到的网页:
任何解决这些问题的任何帮助将不胜感激。
谢谢
卡森
Ps 我正在使用 Url_launcher 包来做到这一点。
小智 16
这是新的更新方式...
whatsapp() async{
var contact = "+880123232333";
var androidUrl = "whatsapp://send?phone=$contact&text=Hi, I need some help";
var iosUrl = "https://wa.me/$contact?text=${Uri.parse('Hi, I need some help')}";
try{
if(Platform.isIOS){
await launchUrl(Uri.parse(iosUrl));
}
else{
await launchUrl(Uri.parse(androidUrl));
}
} on Exception{
EasyLoading.showError('WhatsApp is not installed.');
}
}
Run Code Online (Sandbox Code Playgroud)
并在onpress或ontap函数中调用whatsapp函数
小智 12
我知道现在回答这个问题已经太晚了,但是对于那些想要相同功能的人来说,当前的方法是这样做:
launchUrl(Uri.parse('https://wa.me/$countryCode$contactNo?text=Hi'),
mode: LaunchMode.externalApplication);
Run Code Online (Sandbox Code Playgroud)
截止日期:2022 年 6 月 27 日
使用包: https: //pub.dev/packages/url_launcher
依赖项 - url_launcher: ^6.1.2
TextButton(
onPressed: () {
_launchWhatsapp();
},
)
_launchWhatsapp() async {
var whatsapp = "+91XXXXXXXXXX";
var whatsappAndroid =Uri.parse("whatsapp://send?phone=$whatsapp&text=hello");
if (await canLaunchUrl(whatsappAndroid)) {
await launchUrl(whatsappAndroid);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("WhatsApp is not installed on the device"),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
从官方的 Whatsapp FAQ 中,您可以看到使用“通用链接是链接到 WhatsApp 帐户的首选方法”。
所以在你的代码中,url 字符串应该是:
const url = "https://wa.me/?text=YourTextHere";
Run Code Online (Sandbox Code Playgroud)
如果用户在手机中安装了Whatsapp,此链接将直接打开它。那应该先解决打开网页的问题。
对于无法发送多字消息的问题,那是因为您需要将消息编码为 URL。这在文档中也有说明:
URL-encodedtext 是 URL 编码的预填充消息。
因此,为了在 Dart 中对您的消息进行 url 编码,您可以按如下方式进行:
const url = "https://wa.me/?text=Your Message here";
var encoded = Uri.encodeFull(url);
Run Code Online (Sandbox Code Playgroud)
正如在Dart 语言之旅中看到的那样。
请注意,在您的示例代码中,您在文本消息周围添加了一组额外的单引号,您不应该这样做。
编辑:
Whatsapp FAQ 中还提供的另一个选项是直接使用 Whatsapp Scheme。如果您想尝试,可以使用以下网址:
const url = "whatsapp://send?text=Hello World!"
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您在 iOS9 或更高版本中进行测试,Apple 文档说明:
重要的
如果您的应用在 iOS 9.0 或之后链接,您必须通过将 LSApplicationQueriesSchemes 键添加到您的应用的 Info.plist 文件来声明您传递给此方法的 URL 方案。对于未声明的方案,无论是否安装了适当的应用程序,此方法始终返回 false。要了解有关该键的更多信息,请参阅 LSApplicationQueriesSchemes。
因此,您需要将以下键添加到 info.plist,以防您使用自定义 whatsapp 方案:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Run Code Online (Sandbox Code Playgroud)
要使用wa.me
域,请确保使用此格式...
https://wa.me/123?text=Your Message here
Run Code Online (Sandbox Code Playgroud)
这将发送到电话号码123
。否则,您将收到一条错误消息(请参阅?https://wa.me/ ?text=YourMessageHere )。或者,如果您不想包含电话号码,请尝试此...
https://api.whatsapp.com/send?text=Hello there!
Run Code Online (Sandbox Code Playgroud)
请记住,wa.me
需要电话号码,而不api.whatsapp.com
需要。希望这可以帮助!
归档时间: |
|
查看次数: |
9159 次 |
最近记录: |