如何通过html链接预先填充sms正文文本

时间:2022-03-30 10:29:07

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

我所读到的一切似乎都表明短信:18005555555

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?

我曾看到过一些二维码通过safari链接实现这一功能的例子。如何预填充正文文本?

17 个解决方案

#1


130  

It turns out this is 100% possible, though a little 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>

Link

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

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

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

Link

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: field和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>

Link .

(phone number is optional)

(电话号码是可选的)

#2


34  

For iOS 8, try this:

对于ios8,试试这个:

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

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

切换“;”和“&”为我工作。

#3


28  

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

#4


15  

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 %20 for any spaces.

注意身体链接…我把…;?身体=。另外,请注意,我发现对于任何空格都需要使用%20。

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

我已经在我的iphone(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>

#5


10  

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 - ok(消息正文-“你好,我的朋友”,至“12345678”);
  • HTC Mozart - fault (message "unsupported page" (after click on the "Send sms" link));
  • HTC Mozart - fault(消息“不支持页面”(点击“发送短信”链接));
  • HTC Desire - fault (message "Invalid recipients(s):
    <12345678?body=Hellomyfriend>"(after click on the "Send sms" link)).
  • HTC Desire - fault (message“无效收件人”:<12345678?body=Hellomyfriend>”(点击“发送短信”链接后)。

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

因此我得出结论,至少用这种方法是行不通的。

#6


4  

The iPhone doesn't accept any message text, it will only take in the phone number. You can see this here https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/uid/TP40007899-CH7-SW1

iPhone不接受任何短信,只接受手机号码。您可以在这里看到https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/ TP40007899-CH7-SW1

#7


4  

<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;
})

#8


3  

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 +. 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:$1;');
  });
})();

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

考虑使用类a。sms-link代替[href ^ =“短信:“如果可能的话。

#9


3  

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.
They all worked as expected. They worked without the phone number or email address as well.

我在Chrome上测试了Android和iPhone,在Safari上测试了iPhone。他们都按预期工作。他们工作时没有电话号码或电子邮件地址。

#10


1  

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

This works on my iPhone 5S!

这在我的iPhone 5S上运行!

#11


1  

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;

#12


0  

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.

我发现,在iphone4和ios7上,只有在手机联系人列表中设置了一个电话号码,你才能在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 :

顺便说一下,在ios6 (iPhone 3GS)上,它只需要一个身体:

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

#13


0  

Every OS version has a different way of doing it. Take a look at the sms-link library

每个OS版本都有不同的方法。查看一下sms-link库

#14


0  

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的点击-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或安卓系统上,你可以获得原生的短信功能。

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

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

#15


0  

(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)

#16


0  

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>

//没有联系电话 < / >短信

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

//联系电话 < / >短信

// Works on both Android and iOS

//适用于Android和iOS

#17


-3  

Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. It can be done programmatically though,

Android和iphone目前都不支持短信超链接中的body copy元素。它可以通过编程实现,

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

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

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

#1


130  

It turns out this is 100% possible, though a little 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>

Link

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

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

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

Link

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: field和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>

Link .

(phone number is optional)

(电话号码是可选的)

#2


34  

For iOS 8, try this:

对于ios8,试试这个:

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

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

切换“;”和“&”为我工作。

#3


28  

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

#4


15  

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 %20 for any spaces.

注意身体链接…我把…;?身体=。另外,请注意,我发现对于任何空格都需要使用%20。

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

我已经在我的iphone(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>

#5


10  

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 - ok(消息正文-“你好,我的朋友”,至“12345678”);
  • HTC Mozart - fault (message "unsupported page" (after click on the "Send sms" link));
  • HTC Mozart - fault(消息“不支持页面”(点击“发送短信”链接));
  • HTC Desire - fault (message "Invalid recipients(s):
    <12345678?body=Hellomyfriend>"(after click on the "Send sms" link)).
  • HTC Desire - fault (message“无效收件人”:<12345678?body=Hellomyfriend>”(点击“发送短信”链接后)。

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

因此我得出结论,至少用这种方法是行不通的。

#6


4  

The iPhone doesn't accept any message text, it will only take in the phone number. You can see this here https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/uid/TP40007899-CH7-SW1

iPhone不接受任何短信,只接受手机号码。您可以在这里看到https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html#//apple_ref/doc/ TP40007899-CH7-SW1

#7


4  

<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;
})

#8


3  

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 +. 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:$1;');
  });
})();

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

考虑使用类a。sms-link代替[href ^ =“短信:“如果可能的话。

#9


3  

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.
They all worked as expected. They worked without the phone number or email address as well.

我在Chrome上测试了Android和iPhone,在Safari上测试了iPhone。他们都按预期工作。他们工作时没有电话号码或电子邮件地址。

#10


1  

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

This works on my iPhone 5S!

这在我的iPhone 5S上运行!

#11


1  

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;

#12


0  

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.

我发现,在iphone4和ios7上,只有在手机联系人列表中设置了一个电话号码,你才能在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 :

顺便说一下,在ios6 (iPhone 3GS)上,它只需要一个身体:

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

#13


0  

Every OS version has a different way of doing it. Take a look at the sms-link library

每个OS版本都有不同的方法。查看一下sms-link库

#14


0  

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的点击-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或安卓系统上,你可以获得原生的短信功能。

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

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

#15


0  

(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)

#16


0  

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>

//没有联系电话 < / >短信

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

//联系电话 < / >短信

// Works on both Android and iOS

//适用于Android和iOS

#17


-3  

Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. It can be done programmatically though,

Android和iphone目前都不支持短信超链接中的body copy元素。它可以通过编程实现,

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

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

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