问题
How to use an html link to open the sms app with a pre-filled body?
Everything I have read seems to indicate that 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.
I have seen several instances where QR codes do this through a safari link. How are they able to pre-populate the body text?
回答1:
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:
<a href="sms:/* phone number here */?body=/* body text here */">Link</a>
If you want it to work on iOS, you need this:
<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)
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:
<a href="sms:/* phone number here */&body=/* body text here */">Link</a>
(phone number is optional)
回答2:
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 */
回答3:
For iOS 8, try this:
<a href="sms:/* phone number here */&body=/* body text here */">Link</a>
Switching the ";" with a "&" worked for me.
回答4:
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.
I have tested it on my iphone (v. 9.2) and another android and it works just fine.
This will solve the issue with having to hack it for different devices. I have no artifacts when I tested it in the 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:
There is not need for two separate anchor tags for Android and 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
回答6:
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)).
I therefore conclude it doesn't really work - with this method at least.
回答7:
To get sms: and mailto: links to work on both iPhone and Android, without any javascript, try this:
<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.
回答8:
Bradorego's solution is what worked for me, but here is a more expanded answer.
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.
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.
HTML:
<a href="sms:+15551231234?body=Hello%20World">SMS "Hello World" to 555-123-1234</a>
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.
回答9:
<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;
})
回答10:
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
回答11:
Well not only do you have to worry about iOS and Android, there's also which android messaging app. Google messaging app for Note 9 and some new galaxys do not open with text, but the samsung app works. The solution seems to be add // after the sms:
so sms://15551235555
<a href="sms:/* phone number here */?body=/* body text here */">Link</a>
should be
<a href="sms://15551235555?body=Hello">Link</a>
回答12:
Android and iOS body only:
<a href="sms://;?&body=Hello%20World">Only body</a>
Android and iOS one recipient only with body:
<a href="sms://+15552345678;?&body=Hello%20World">one recipient only with body</a>
Only Android multiple recipients with body:
<a href="sms://+15552345678, +15552345679;?&body=Hello%20World">Android multiple recipients with body</a>
Only iOS multiple recipients with body:
<a href="sms://open?addresses=+15552345678,+15552345679;?&body=Hello%20World">iOS multiple recipients with body</a>
Note that the body should be URI encoded.
回答13:
<a href="sms:/* phone number here */&body=/* body text here */">Link</a>
This works on my iPhone 5S!
回答14:
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:
sms:?body=message
For iOS same thing except with the ;
sms:;body=message
Here's an example of the code I use to set up the 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;
回答15:
(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 :
[Link](sms:phone_number,?&body=URL_encoded_body_text)
//[send sms](sms:1234567890;?&body=my%20very%20interesting%20text)
回答16:
For using Android you use below code
<a href="sms:+32665?body=reg fb1>Send SMS</a>
For iOS you can use below code
<a href="sms:+32665&body=reg fb1>Send SMS</a>
below code working for both iOs and Android
<a href="sms:+32665?&body=reg fb1>Send SMS</a>
回答17:
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.
So the following will work If 0606060606 is part of my contacts:
<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 :
<a href="sms:;body=Hello my friend">Send SMS</a>
回答18:
Every OS version has a different way of doing it. Take a look at the sms-link library
回答19:
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.
- 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.
https://www.zipwhip.com/create-sms-button/
回答20:
Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. 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];
来源:https://stackoverflow.com/questions/6480462/how-to-pre-populate-the-sms-body-text-via-an-html-link