当短信应用程序不在后台 运行 时,无法在 iPhone 上的短信 link 中预填充 phone 号码和消息正文

Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

HTML 允许您使用此 link:

轻松地与 SMS 应用程序交互
<a href="sms:">Send a SMS</a>

但是不同的 OS 允许您还使用以下方式预填充 phone 号码和消息正文:

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

在 Android 或

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

在 iOS 8+

这在this question.

中都有很好的解释

但是 我注意到 iPhones 上的一个问题,我似乎找不到解决方案:如果 SMS 应用程序不是 运行在 iPhone 的后台,单击 link 将打开短信应用程序 ,但不会在新消息中预先填充 phone 号码和消息正文。

由于 Google AdWords 正在使用 this functionality too,我也测试了他们的 links 但不幸的是他们遇到了同样的问题,所以我怀疑是否有解决方案但是仍然想在这里与社区核实。

你应该使用 Apple 的 MessageUI 包

   import MessageUI

并在viewController里面写下这段代码来发送消息

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

不要忘记实现委托函数 //MARK:- MessageUI 代表

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

几年前我向 Apple 报告了一个错误,我相信他们已经修复了它。为了支持旧的 iOS 版本,我只是应用了一个简单的脚本,它将点击 link,等待半秒然后再次点击:

<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>

其中 openSMS.php 是:

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