如何使用带有flutter的URL_launcher包发送短信? [英] How to send sms with URL_launcher package with flutter?
问题描述
您好,我搜索了一个简单的示例(Android和iOS),以与此包一起发送短信
Hello I search a simple example (Android and iOS) to send SMS with this package
https://pub.dartlang.org/packages/url_launcher
在插件页面中,我仅看到如何使用电话号码打开短信本机应用程序,但没有多余的消息
In the plugin page I only see how to open sms native app with phone number, but no extra message
sms:<phone number>, e.g. sms:5550101234 Send an SMS message to <phone
number> using the default messaging app
推荐答案
在Android上,完整的sms:
URI受支持,您可以发送带有这样的正文的消息(
On Android the full sms:
URI is supported and you can send a message with a body like that (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';
}
}
}
On iOS the official doc says you can only use the number field of The URI
.
代替 Konstantine 所指出的那样,如果您使用非标准的URI
,并且使用&
代替,而不使用?
开始查询字符串,则仍然可以正常使用.似乎是一个未记录的功能.
Instead as Konstantine pointed out, if you use a non standard URI
and instead and instead of starting the query string with ?
you use &
it still works as well. It seems like an undocumented feature.
短信计划用于启动消息"应用.网址格式 这种类型是"sms:",其中是可选参数 指定SMS消息的目标电话号码.这 参数可以包含数字0到9和加号(+),连字符 (-)和句点(.)字符. URL字符串不得包含任何 消息文字或其他信息.
The sms scheme is used to launch the Messages app. The format for URLs of this type is "sms:", where is an optional parameter that specifies the target phone number of the SMS message. This parameter can contain the digits 0 through 9 and the plus (+), hyphen (-), and period (.) characters. The URL string must not include any message text or other information.
PS.要检查平台,您可以使用 dart.io库Platform
类:
PS. to check the plaform you could use the dart.io library Platform
class:
_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);
}
}
这篇关于如何使用带有flutter的URL_launcher包发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!