Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Prefilling an SMS on Mobile Devices with the sms: Uri Scheme


:P
On this page:

Popping up the native SMS app from a mobile HTML Web page is a nice feature that allows you to pre-fill info into a text for sending by a user of your mobile site. The syntax is a bit tricky due to some device inconsistencies (and quite a bit of wrong/incomplete info on the Web), but it's definitely something that's fairly easy to do.

In one of my Mobile HTML Web apps I need to share a current location via SMS. While browsing around a page I want to select a geo location, then share it by texting it to somebody. Basically I want to pre-fill an SMS message with some text, but no name or number, which instead will be filled in by the user.

What works

The syntax that seems to work fairly consistently except for iOS is this:

sms:phonenumber?body=message

For iOS instead of the ? use a ';' (because Apple is always right, standards be damned, right?):

sms:phonenumber;body=message

and that works to pop up a new SMS message on the mobile device. I've only marginally tested this with a few devices: an iPhone running iOS 6, an iPad running iOS 7, Windows Phone 8 and a Nexus S in the Android Emulator. All four devices managed to pop up the SMS with the data prefilled.

You can use this in a link:

<a href="sms:1-111-1111;body=I made it!">Send location via SMS</a>

or you can set it on the window.location in JavaScript:

window.location ="sms:1-111-1111;body=" + encodeURIComponent("I made it!");

to make the window pop up directly from code. Notice that the content should be URL encoded - HTML links automatically encode, but when you assign the URL directly in code the text value should be encoded.

Body only

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;

and that also works for all the devices mentioned above.

It's pretty cool that URL schemes exist to access device functionality and the SMS one will come in pretty handy for a number of things. Now if only all of the URI schemes were a bit more consistent (damn you Apple!) across devices...

Posted in IOS  JavaScript  HTML5  

The Voices of Reason


 

Red Feet
October 10, 2013

# re: using 'sms:' Uri Scheme without specifying the phonenumber

Thanks Rick,

For figuring it out and sharing your findings!

I refactored the javascript to this:

var ua = navigator.userAgent.toLowerCase();
var url = "sms:";
url += (ua.indexOf("iphone") > -1 || ua.indexOf("ipad") > -1) ? ";" : "?";
url += "body=" + encodeURIComponent("I'm at " + mapUrl + " @ " + pos.Address);
location.href = url;

ez
October 10, 2013

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

For me (iPhone 4s with iOS7) this does not prefill the body; however on an older iphone with iOS6 it does work for me. Anyone else with iOS7 iphone try this?

Rick Strahl
October 10, 2013

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

@ez - I tried this on iPad with iOS 7 and that seemed to work. I assume this is an OS feature not a hardware feature. If it doesn't work - can you try the alternate (proper) syntax with the ?

ez
October 11, 2013

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

I tried it both ways and the body does not prefil.

Rick Strahl
October 11, 2013

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

@ez - not sure definitely works on iOS7 with my iPad 2. Maybe somebody else can chime in from iOS 7. I just can't see that this would work different on any specific Apple device since this is an OS level feature. Maybe there are some special security settings at work? Can you 'Share' to SMS from other apps?

Noam Shapira
October 23, 2013

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

As far as I see on iPhone5, sms body is prefil only when old conversion to same target exist in the messaging history of the phone.

David
February 14, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

ez - same thing happening to me in IOS7 Iphone 5. Did you ever get a solution to this?

Rupert
March 13, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

Just picking up on this thread, it has been incredibly useful. I can confirm however, that prefil seems only to work if a previous conversation thread exists to a particular number.
If there is a known work around to this I would love to know because this could be a big breaker for us in terms of an idea we have to utilise this functionality.

Thanks a lot,
Rupert

Roy
June 09, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

Hi everyeone,

I came to the same conclusion as everyone else - Body is only prefilled if there is a pre-existing thread to the person.

Has anyone found a workaround?

Many thanks

Brandon
June 25, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

For some reason sms:?body= links will not open correctly from chrome on android, but just fine from Firefox on android. I am using Hangouts for SMS. Any ideas on how to fix?

Ravi
August 28, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

I am not seeing the pre-filled text if I don't pass the phone number, I don't know the phone number upfront, we want the text to appear and let the user to choose whomever they need to send it to. So far I was not successful.

sms:;body=message is not working as expected on iOS 7.1.1. Any workarounds?

Thanks in advance

Frustrated
September 13, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

not working on IO7, no fix that I know of. Sucks. If any one has any luck or comes up with workaround, please post.

danielsdesk
November 04, 2014

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

FYI for iOS 8, you have to use an & instead of ; now and body prefill will work... however it still seems to be impossible on iOS7 (so basically iOS 5-6 and 8 can prefill body)

JĂșlio
March 14, 2015

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

This post helped me figure out how to get SMS URIs to work.

Here’s a little follow up: http://blog.julianklotz.de/2015/03/14/the-sms-uri-scheme/

Amin
December 31, 2015

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

The method you mentioned is working very well on Android. But when I have tried to use it on Windows Phone 8.1, the Mobile phone is pop up a message with title: "Search for app in the store?"
Do you have any idea, what is going wrong?


Thank you in advanced.

Rick Strahl
December 31, 2015

# re: Prefilling an SMS on Mobile Devices with the sms: Uri Scheme

Windows phone doesn't support the sms: moniker. AFAIK there's no way to do this from a Web app - you have to use a native app and native APIs to get SMS to pop up on WinMobile.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024