Trying to send sms in iOS 10, Is “sms:” protocol b

2020-05-27 15:15发布

问题:

I have a click-to-send-sms button.

Now I'm using this code when the button is clicked:

if (platform == 'iOS') {
    if (version == 4 || version == 5 || version == 6 || version == 7) {
        link = 'sms:' + serviceNumber + ';body=' + body;
    } else {
        link = 'sms:' + serviceNumber + '&body=' + body;
    }
} else {
    link = 'sms:' + serviceNumber + '?body=' + encodeURIComponent(body);
}
window.location.href = link;

They are telling me that it isn't working anymore in iOS 10, nothing happens when the button is clicked. (the problem is not in the UA recognition, it goes into the "&body=...")

Currently I don't have an ios 10 device to debug... did they change the way to open the SMS outbox? Maybe I have to use encodeURIcomponent for the body like android/windows?

回答1:

It seems not. Just tested this on my iPhone 6+ running iOS 10.0.1

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>test</title>
  </head>
  <body>
    <a href="sms:0412345678&body=testing">send sms</a>
    <button onclick="window.location.href = 'sms:0412345678&body=testing'">send sms</button>
  </body>
</html>

Both clicking the link and the button worked perfectly, opened up the Messages app with the correct number and added the text to the body.

Adding spaces, numbers and symbols works. Although standard URI components apply (adding %20 adds a space for example). So I would recommend sanitising the body with encodeURIComponent.


From what I can tell it seems Apple hasn't even updated their documentation on this:

The sms scheme is used to launch the Messages app. The format for URLs of this type is “sms:”, where is an optional parameter that specifies the target phone number of the SMS message. This parameter can contain the digits 0 through 9 and the plus (+), hyphen (-), and period (.) characters. The URL string must not include any message text or other information.

However, the body parameter is obviously working.

iOS < 7 <a href="sms:0412345678;body=text">send sms</a>
iOS > 7 <a href="sms:0412345678&body=text">send sms</a>

After testing I've confirmed that <a href="sms:0412345678&body=test">send sms</a> works on:

  • iPhone 6+ (iOS 10.0.1)
  • iPhone 6 (iOS 10)
  • iPhone 5 (iOS 9)
  • iPhone 4S (iOS 8)