6
votes

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.

2
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
@Ł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

2 Answers

0
votes

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)
   }
 }
0
votes

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