简体   繁体   English

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

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

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.应该可以,但在 iPhone 上,这不起作用。 If I take out the?body=bodyTextHere, and just use sms:phonenumber, it works.如果我取出?body=bodyTextHere,只使用sms:phonenumber,它就可以工作。

I have seen several instances where QR codes do this through a safari link.我已经看到几个 QR 码通过 safari 链接执行此操作的实例。 How are they able to pre-populate the body text?他们如何能够预填充正文?

It turns out this is 100% possible, though a little hacky.事实证明这是 100% 可能的,虽然有点 hacky。

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>

If you want it to work on iOS, you need this:如果你想让它在 iOS 上工作,你需要这个:

<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”应该自动填充到:字段和正文文本。查看源代码以获取更多信息信息)

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>

(phone number is optional) (电话号码是可选的)

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 */

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.用“&”为我工作。

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= .注意正文链接...我只是放了... ;?&body= Also, notice that I have found I needed to use %20 for any spaces.另外,请注意,我发现我需要对任何空格使用%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>

There is not need for two separate anchor tags for Android and iOS. Android 和 iOS 不需要两个单独的锚标签。 This should help.这应该会有所帮助。

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

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

// Works on both Android and iOS // 适用于 Android 和 iOS

Android and iOS body only:仅限Android 和 iOS机身:

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

Android and iOS one recipient only with body: Android 和 iOS一个收件人只有正文:

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

Only Android multiple recipients with body:仅具有正文的 Android多个收件人:

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

Only iOS multiple 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 编码的。

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

I therefore conclude it doesn't really work - with this method at least.因此,我得出结论,它并没有真正起作用——至少使用这种方法。

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:someone@sample.com?&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.我在适用于 Android 和 iPhone 的 Chrome 以及 iPhone 上的 Safari 上对其进行了测试。
They all worked as expected.他们都按预期工作。 They worked without the phone number or email address as well.他们也可以在没有电话号码或电子邮件地址的情况下工作。

For those wanting a solution that works in 2022 I set up a small web app that just uses the correct format for iOS, macOS, and Android automatically.对于那些想要在 2022 年工作的解决方案的人,我设置了一个小型 Web 应用程序,它只自动使用适用于 iOS、macOS 和 Android 的正确格式。

https://copy.gives/18005555555?body=Body+text+here

The issue in 2022 is that on iOS you * must * use double slashes as well as /& 2022 年的问题是,在 iOS 上,您 * 必须 * 使用双斜杠以及/&

So the respective working URLs are the following所以各自的工作网址如下

sms://18005555555/&body=Body%20text%20here - iOS and macOS
sms://18005555555/?body=Body%20text%20here - Android

Notice the /& for Apple devices and /?注意 Apple 设备的/&/? for Android安卓版

You can try some working examples here(doesn't work embedded):您可以在这里尝试一些工作示例(不能嵌入工作):

https://jsfiddle.net/thatguysam/swLtjh0q/ https://jsfiddle.net/thatguysam/swLtjh0q/

For using Android you use below code对于使用 Android,您使用以下代码

<a href="sms:+32665?body=reg fb1>Send SMS</a>

For iOS you can use below code对于 iOS,您可以使用以下代码

<a href="sms:+32665&body=reg fb1>Send SMS</a>

below code working for both iOs and Android以下代码适用于 iOs 和 Android

<a href="sms:+32665?&body=reg fb1>Send SMS</a>
<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;
})

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 %20 instead of + .一个小的考虑是您需要使用%20而不是+对正文进行编码。 For PHP, this means using rawurlencode($body) instead of urlencode($body) .对于 PHP,这意味着使用rawurlencode($body)而不是urlencode($body) Otherwise you'll see plus signs in the message on old versions of iOS, instead of spaces.否则,您将在旧版本的 iOS 上看到消息中的加号,而不是空格。

Here is a jQuery function which will refit your SMS links for iOS devices.这是一个 jQuery 函数,它将为 iOS 设备重新调整您的 SMS 链接。 Android/other devices should work normally and won't execute the code. 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:$1;');
  });
})();

Consider using a class like a.sms-link instead of a[href^="sms:"] if possible.如果可能,请考虑使用a.sms-link之类的类而不是a[href^="sms:"]

The iPhone doesn't accept any message text, it will only take in the phone number. iPhone 不接受任何消息文本,它只会接收电话号码。 You can see this here https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/uid/TP40007899-CH7-SW1你可以在这里看到这个https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/uid/TP40007899-CH7-SW1

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

这适用于我的 iPhone 5S!

I suspect in most applications you won't know who to text, so you only want to fill the text body, not the number.我怀疑在大多数应用程序中你不知道给谁发短信,所以你只想填写文本正文,而不是数字。 That works as you'd expect by just leaving out the number - here's what the URLs look like in that case:只需省略数字即可按预期工作 - 这是这种情况下 URL 的样子:

sms:?body=message

For iOS same thing except with the ;对于 iOS 相同的东西,除了 ;

sms:;body=message

Here's an example of the code I use to set up the SMS:这是我用来设置 SMS 的代码示例:

var ua = navigator.userAgent.toLowerCase();
var url;

if (ua.indexOf("iphone") > -1 || ua.indexOf("ipad") > -1)
   url = "sms:;body=" + encodeURIComponent("I'm at " + mapUrl + " @ " + pos.Address);
else
   url = "sms:?body=" + encodeURIComponent("I'm at " + mapUrl + " @ " + pos.Address);

   location.href = url;

Well not only do you have to worry about iOS and Android, there's also which android messaging app.好吧,您不仅要担心 iOS 和 Android,还有哪个 android 消息传递应用程序。 Google messaging app for Note 9 and some new galaxys do not open with text, but the samsung app works.用于 Note 9 和一些新星系的谷歌消息应用程序不会以文本打开,但三星应用程序可以工作。 The solution seems to be add // after the sms:解决方案似乎是在短信之后添加//:

so sms://15551235555所以短信://15551235555

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

should be应该

<a href="sms://15551235555?body=Hello">Link</a>

(Just a little bit of topic), but maybe if you searched you could stumble here... In markdown (tested with parsedown and on iOS / android) you could do : (只是一点点主题),但也许如果你搜索你可能会在这里绊倒......在markdown (用parsedown和iOS / android测试)你可以这样做:

   [Link](sms:phone_number,?&body=URL_encoded_body_text)
   //[send sms](sms:1234567890;?&body=my%20very%20interesting%20text)

Was struggling with ability to open SMS app with body only message, no recipients on iOS 11+.在 iOS 11+ 上无法使用仅正文消息打开 SMS 应用程序而苦苦挣扎,没有收件人。

None of the solutions above worked for me, it didn't open at all or opened with something pre-populated in recipients (like ';').上面的解决方案都不适合我,它根本没有打开,也没有打开收件人中预先填充的东西(比如';')。

Finally I ended up with this syntax for body only:最后,我最终得到了这种仅适用于正文的语法:

sms:///?body=Hello%20World

Used up to iOS 14, worked fine!使用到 iOS 14,运行良好!

?body= //for Android
&body= //for iOS

I found out that, on iPhone 4 with IOS 7 , you CAN put a body to the SMS only if you set a phone number in the list of contact of the phone.我发现,在带有 IOS 7 的 iPhone 4 上,只有在手机的联系人列表中设置了电话号码,才能将正文放入 SMS。

So the following will work If 0606060606 is part of my contacts:因此,如果 0606060606 是我的联系人的一部分,则以下内容将起作用:

<a href="sms:0606060606;body=Hello my friend">Send SMS</a>

By the way, on iOS 6 (iPhone 3GS), it's working with just a body :顺便说一句,在iOS 6 (iPhone 3GS) 上,它只使用一个 body :

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

Every OS version has a different way of doing it.每个操作系统版本都有不同的方法。 Take a look at the sms-link library看看sms-link 库

universal link use tel &?通用链接使用 tel &? body身体

<a href="sms://+123456789&?body=url works on android and also ios">link ios+android</a>

https://output.jsbin.com/puqicel https://output.jsbin.com/puqicel

link ios+android链接ios+安卓

 <a href="sms://+123456789&?body=url works on android and also ios">link ios+android</a>

One of the problems with a click-to-text link is solving the desktop scenario where no native texting app exists.点击文本链接的问题之一是解决不存在本机短信应用程序的桌面场景。 A solution is to use Zipwhip's Click-to-Text button creator.一个解决方案是使用 Zipwhip 的 Click-to-Text 按钮创建器。

  • On the desktop, they send you an actual real text message behind the scenes from the user's input.在桌面上,它们会根据用户的输入在幕后向您发送一条真实的文本消息。
  • On iOS or Android you get the native texting functionality instead.在 iOS 或 Android 上,您将获得本机短信功能。

https://www.zipwhip.com/create-sms-button/ https://www.zipwhip.com/create-sms-button/

Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. Android 和 iPhone 目前都不支持 Tap to SMS 超链接中的正文复制元素。 It can be done programmatically though,它可以以编程方式完成,

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

picker.recipients = [NSArray arrayWithObject:@"48151623"];  
picker.body = @"Body text.";

[self presentModalViewController:picker animated:YES];
[picker release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM