flutter - 如何用颤振的 URL_launcher 包发送短信?
问题描述
你好我搜索一个简单的例子(Android和iOS)用这个包发送短信
https://pub.dartlang.org/packages/url_launcher
在插件页面中,我只看到如何使用电话号码打开短信本机应用程序,但没有额外消息
sms:<phone number>, e.g. sms:5550101234 Send an SMS message to <phone
number> using the default messaging app
解决方案
在 Android 上,sms:
支持完整的 URI,您可以发送带有类似正文 ( RFC5724 ) 的消息:
_textMe() async {
// Android
const uri = 'sms:+39 348 060 888?body=hello%20there';
if (await canLaunch(uri)) {
await launch(uri);
} else {
// iOS
const uri = 'sms:0039-222-060-888?body=hello%20there';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
}
在iOS上,官方文档说您只能使用 The 的数字字段URI
。
相反,正如Konstantine指出的那样,如果您使用非标准URI
,而不是使用?
您开始查询字符串,&
它仍然可以正常工作。这似乎是一个未记录的功能。
短信方案用于启动消息应用程序。此类型 URL 的格式为“sms:”,其中是一个可选参数,用于指定 SMS 消息的目标电话号码。此参数可以包含数字 0 到 9 以及加号 (+)、连字符 (-) 和句点 (.) 字符。URL 字符串不得包含任何消息文本或其他信息。
PS。要检查平台,您可以使用dart.io 库Platform
类:
_textMe() async {
if (Platform.isAndroid) {
const uri = 'sms:+39 348 060 888?body=hello%20there';
await launch(uri);
} else if (Platform.isIOS) {
// iOS
const uri = 'sms:0039-222-060-888&body=hello%20there';
await launch(uri);
}
}
推荐阅读
- reactjs - onChange 反应事件调用带有参数但没有传递参数的函数?
- plsql - 事务范围之外的查询 - Oracle
- react-native - 如何在本机反应中使用 Slider 使光标跟随光标
- python - Python Selenium (Chrome Webdriver) - 根据值定义 URL
- php - Maria DB SQL Insert INTO ON DUPLICATE 仅适用于第一个更新的值
- jquery - 将 Bootstrap 与 React 一起使用:Bootstrap 下拉菜单未打开
- angular - Angular 7,ng-bootstrap 渲染模态模板
- xcode - 2 个 Xcode 目标之间的循环依赖
- django - 我尝试在 Django 管理面板中存储用户 ID,例如 laravel
- tsql - SSIS 死锁,因为需要在同一数据流任务中进行并行更新