如何通过 html 链接预填充短信正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6480462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:15:34  来源:igfitidea点击:

How to pre-populate the sms body text via an html link

htmlioshyperlinksms

提问by merlincam

How to use an html link to open the sms app with a pre-filled body?

如何使用 html 链接打开带有预填充正文的短信应用程序?

Everything I have read seems to indicate that sms:18005555555?body=bodyTextHere

我读过的一切似乎都表明 sms:18005555555?body=bodyTextHere

Should work, but on the iPhone, this doesn't work. If I take out the ?body=bodyTextHere, and just use sms:phonenumber, it works.

应该可以,但在 iPhone 上,这行不通。如果我取出 ?body=bodyTextHere,然后只使用 sms:phonenumber,它就可以工作。

I have seen several instances where QR codes do this through a safari link. How are they able to pre-populate the body text?

我见过几个例子,其中 QR 码通过 safari 链接执行此操作。他们如何能够预先填充正文?

回答by Brad Orego

It turns out this is 100% possible, though a little hacky.

事实证明,这是 100% 可能的,尽管有点老套。

If you want it to work on Android you need to use this format:

如果您希望它在 Android 上运行,则需要使用以下格式:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

If you want it to work on iOS, you need this:

如果你想让它在 iOS 上运行,你需要这个:

<a href="sms:/* phone number here */;body=/* body text here */">Link</a>

<a href="sms:/* phone number here */;body=/* body text here */">Link</a>

Live demo here: http://bradorego.com/test/sms.html(note the "Phone and ?body" and "Phone and ;body" should autofill both the to: field and the body text. View the source for more info)

现场演示:http: //bradorego.com/test/sms.html(注意“Phone and ?body”和“Phone and ;body”应该自动填充 to: 字段和正文文本。查看源以获取更多信息信息)

UPDATE:

更新:

Apparently iOS8 had to go and change things on us, so thanks to some of the other commenters/responders, there's a new style for iOS:

显然 iOS8 不得不改变我们的事情,所以感谢其他一些评论者/回复者,iOS 有了一种新风格:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

(phone number is optional)

(电话号码是可选的)

回答by Ali

I know this is an old thread but stumbled upon it and found that some parts are no longer relevant.

我知道这是一个旧线程,但偶然发现它并发现某些部分不再相关。

I've found that if you want to just per-populate the text without adding a phone number, you can do the following:

我发现如果您只想填充文本而不添加电话号码,您可以执行以下操作:

sms:?&body=/* message body here */

回答by landen

For iOS 8, try this:

对于 iOS 8,试试这个:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

Switching the ";" with a "&" worked for me.

切换“;” 带有“&”对我有用。

回答by Nathan Sharfi

Just put all of the symbols in this order (I only tested it in this order since it makes the most sense code-wise to me).

只需按此顺序放置所有符号(我仅按此顺序对其进行了测试,因为它对我来说在代码方面最有意义)。

Notice for the body link... I just put... ;?&body=. Also, notice that I have found I needed to use %20for any spaces.

对于身体链接通知...我只是把... ;?&body=。另外,请注意我发现我需要%20用于任何空间。

I have tested it on my iphone (v. 9.2) and another android and it works just fine.

我已经在我的 iphone (v. 9.2) 和另一个 android 上测试过它,它工作得很好。

This will solve the issue with having to hack it for different devices. I have no artifacts when I tested it in the SMS.

这将解决必须针对不同设备破解它的问题。当我在 SMS 中测试它时,我没有任何工件。

<a href="sms:19131234567;?&body=Question%20from%20mywebsite.com.%20%20MY%20MESSAGE%20-%20" title="Click here to TEXT US gallery token needs updating!">Send me SMS</a>

回答by aayushdrolia

There is not need for two separate anchor tags for Android and iOS. This should help.

Android 和 iOS 不需要两个单独的锚标记。这应该有帮助。

// Without Contact Number
<a href="sms:?&body=message">Text Message</a>

// 没有联系电话
<a href="sms:?&body=message">Text Message</a>

// With Contact Number
<a href="sms:1234567890;?&body=message">Text Message</a>

// 有联系电话
<a href="sms:1234567890;?&body=message">Text Message</a>

// Works on both Android and iOS

// 适用于 Android 和 iOS

回答by Ben

We found a proposed method and tested:

我们找到了一种建议的方法并进行了测试:

<a href="sms:12345678?body=Hello my friend">Send SMS</a>

Here are the results:

结果如下:

  • iPhone4 - fault (empty body of message);
  • Nokia N8 - ok (body of message - "Hello my friend", To "12345678");
  • HTC Mozart - fault (message "unsupported page" (after click on the "Send sms" link));
  • HTC Desire - fault (message "Invalid recipients(s):
    <12345678?body=Hellomyfriend>"(after click on the "Send sms" link)).
  • iPhone4 - 故障(消息体为空);
  • 诺基亚 N8 - 好的(消息正文 - “你好,我的朋友”,至“12345678”);
  • HTC Mozart - 故障(消息“不支持的页面”(点击“发送短信”链接后));
  • HTC Desire - 故障(消息“无效的收件人:
    <12345678?body=Hellomyfriend>”(点击“发送短信”链接后))。

I therefore conclude it doesn't really work - with this method at least.

因此,我得出结论它并没有真正起作用 - 至少使用这种方法。

回答by MahMoos

Android and iOSbody only:

仅限Android 和 iOS机身:

<a href="sms://;?&body=Hello%20World">Only body</a>

Android and iOSone recipient only with body:

Android 和 iOS一个收件人只有正文:

<a href="sms://+15552345678;?&body=Hello%20World">one recipient only with body</a>

Only Androidmultiple recipients with body:

只有带有正文的Android多个收件人:

<a href="sms://+15552345678, +15552345679;?&body=Hello%20World">Android multiple recipients with body</a>

Only iOSmultiple recipients with body:

仅 iOS多个收件人与正文:

<a href="sms://open?addresses=+15552345678,+15552345679;?&body=Hello%20World">iOS multiple recipients with body</a>

Note that the body should be URI encoded.

请注意,正文应该是 URI 编码的。

回答by Debbie V

To get sms: and mailto: links to work on both iPhone and Android, without any javascript, try this:

要获取 sms: 和 mailto: 链接以在 iPhone 和 Android 上运行,无需任何 JavaScript,请尝试以下操作:

<a href="sms:321-555-1111?&body=This is what I want to sent">click to text</a>


<a href="mailto:[email protected]?&subject=My subject&body=This is what I want to sent">click to email</a>

I tested it on Chrome for Android & iPhone, and Safari on iPhone.
They all worked as expected. They worked without the phone number or email address as well.

我在适用于 Android 和 iPhone 的 Chrome 以及 iPhone 上的 Safari 上对其进行了测试。
他们都按预期工作。他们也没有电话号码或电子邮件地址。

回答by Radley Sustaire

Bradorego's solution is what worked for me, but here is a more expanded answer.

Bradorego 的解决方案对我有用,但这里有一个更广泛的答案。

A small consideration is that you need to encode the body using %20instead of +. For PHP, this means using rawurlencode($body)instead of urlencode($body). Otherwise you'll see plus signs in the message on old versions of iOS, instead of spaces.

一个小考虑是您需要使用%20而不是+. 对于 PHP,这意味着使用rawurlencode($body)代替urlencode($body). 否则,您会在旧版 iOS 的消息中看到加号,而不是空格。

Here is a jQuery function which will refit your SMS links for iOS devices. Android/other devices should work normally and won't execute the code.

这是一个 jQuery 函数,它将为 iOS 设备改装您的 SMS 链接。Android/其他设备应该可以正常工作,不会执行代码。

HTML:

HTML:

<a href="sms:+15551231234?body=Hello%20World">SMS "Hello World" to 555-123-1234</a>

jQuery:

jQuery:

(function() {
  if ( !navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ) return;

  jQuery('a[href^="sms:"]').attr('href', function() {
    // Convert: sms:+000?body=example
    // To iOS:  sms:+000;body=example (semicolon, not question mark)
    return jQuery(this).attr('href').replace(/sms:(\+?([0-9]*))?\?/, 'sms:;');
  });
})();

Consider using a class like a.sms-linkinstead of a[href^="sms:"]if possible.

如果可能,请考虑使用类似的类a.sms-link而不是a[href^="sms:"]

回答by hkongm

<a href="###" data-telno="13800000000" data-smscontent="hello" class="XXXXX XXXXXX XXXXXX sendsms"/>

$('.sendsms').on('click', function(){
    var p = $(this).data('telno'),
        c = $(this).data('smscontent'),
        t = ';';

    if (!ios) { // add your own iOS check
        t = '?';
    }
    location.href = 'sms:'+ p + t + c;
})