当短信应用未在后台运行时,无法在iPhone上的短信链接中预先填充电话号码和邮件正文

时间:2017-10-20 14:13:49

标签: html ios iphone hyperlink sms

HTML允许您使用以下链接轻松与SMS应用进行交互:

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

但是,不同的操作系统允许您使用以下方法预先填充电话号码和邮件正文:

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

中得到了很好的解释

然而我发现iPhone上的问题似乎无法找到解决方案:如果您的iPhone上没有在后台运行短信应用,点击该链接就会打开短信应用但不会在新邮件中预先填充电话号码和邮件正文。

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

2 个答案:

答案 0 :(得分:1)

你应该使用Apple的MessageUI Package

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

答案 1 :(得分:0)

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

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