6

HTML allows you to easily interact with the SMS app using this link:

<a href="sms:">Send a SMS</a>

However different OS allows you to also pre-populate the phone number and message body using:

<a href="sms:1234567890?body=Pre-Populted%20Message">Link</a>

on Android, or

<a href="sms:1234567890&body=Pre-Populted%20Message">Link</a>

On iOS 8+

This is all well explained in this question.

However I noticed a problem on iPhones that I can't seem to find a solution for: If the SMS app is not running in the background on your iPhone, clicking the link will open the SMS app but will not pre-populate the phone number and message body in a new message.

Since Google AdWords are using this functionality too, I tested their links too but unfortunately they are suffering from the same problem, so I doubt there is a solution out there but still wanted to check with the community here.

DMEM
  • 1,573
  • 8
  • 25
  • 43
  • Hey, it's almost 3 years and I still have the same issue as you did. Have you tried reporting it to Apple? Have you manage to solve it somehow? – Łukasz Jagodziński Aug 17 '20 at 21:53
  • @ŁukaszJagodziński see my answer below. It should be fixed on newer iOS versions but if it's not, you can try to proposed solution I used. – DMEM Aug 20 '20 at 19:54

2 Answers2

0

you should use MessageUI Package of Apple

   import MessageUI

and inside viewController write this code to send a message

   if MFMessageComposeViewController.canSendText() {
            let vc = MFMessageComposeViewController()
            vc.messageComposeDelegate = self
            vc.recipients = ["PHONE_NUMBER"]
            vc.body = "MESSAGE_BODY"
            self.presentVC(vc)
   }

Don't forget to implement delegate functions //MARK:- MessageUI Delegates

extension VIEWCONTROLLER_CLASS: MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate {
   func messageComposeViewController(_ controller: 
   MFMessageComposeViewController, didFinishWith result: 
   MessageComposeResult) {
          switch result {
           case .cancelled:
               controller.dismiss(animated: true, completion: nil)
           case .sent:
               controller.dismiss(animated: true, completion: nil)
           case .failed:
               controller.dismiss(animated: true, completion: nil)
          }
    }

   func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
          controller.dismiss(animated: true, completion: nil)
   }
 }
Sand'sHell811
  • 358
  • 3
  • 15
0

I reported a bug to Apple a few years ago and I believe they fixed it since. To support older iOS versions, I just applied a simple script that will hit the link, wait half a second and then hit it again:

<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<a href="#" onclick="window.open('openSMS.php?number=5556667777&text=TEXT_TO_PRE_POPULATE');
                      wait(500);
                      window.open('openSMS.php?number=5556667777&text=TEXT_TO_PRE_POPULATE');">
Click here to open the SMS app
</a>
    
</body>
</html>

where openSMS.php is:

<?php

        // Include and instantiate the class.
        require_once 'Mobile-Detect-2.8.25/Mobile_Detect.php';
        $detect = new Mobile_Detect;
        
        // Get parameters
        $number = $_GET['number'];
        $text = $_GET['text'];
        $url;
         
        // Check for a specific platform with the help of the magic methods:
        if( $detect->isiOS() ){
            $url = "sms:" . $number . "&body=" . $text;         
        } else if( $detect->isAndroidOS() ){
            $url = "sms:" . $number . "?body=" . $text;
        } else if ($detect->isMobile()){
            $url = "sms:" . $number . "?body=" . $text;
        } else {
            
        }
        
        header('Location: '.$url);
?>
DMEM
  • 1,573
  • 8
  • 25
  • 43
  • Unfortunately waiting for some time and clicking link programmatically again doesn't work. You can't execute JS when you leave browser. So it will only work when you get back to the browser. Then it will ask to open link again which is far from perfect and also it will only work in Safari. It didn't work in Chrome iOS. Apple didn't fix this bug which is at least 3 years old. They don't give a shit about developers... – Łukasz Jagodziński Aug 26 '20 at 17:48
  • Oh actually I have to say sorry to Apple as they fixed this bug in the latest iOS update that I've installed today. They actually listen to users :). Important thing to note is that I've reported it as the Messages app issue. Previously most bugs were reported as web bugs and there nothing was fixed. But it looks like the Messages app team is much more helpful :) – Łukasz Jagodziński Aug 26 '20 at 18:26