向 Swift 中的多个收件人发送短信

Send SMS Message to multiple recipients in Swift

我想使用 MFMessageComposeViewController 向多个收件人发送一条消息,我无法添加第二个等收件人

这是我的代码:

@IBAction func sendSms(sender: AnyObject) {

        if (MFMessageComposeViewController.canSendText())
        {
            let controller = MFMessageComposeViewController()
            controller.body = self.textView.text
            controller.recipients = [self.phoneField.text!]
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
        else
        {
            print("Error")
        }
    }

extension MyViewController : MFMessageComposeViewControllerDelegate 
{
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult)     
    {
            controller.dismissViewControllerAnimated(true, completion: nil)}
    }
}

关于如何将其发送给多个收件人的任何建议?

如果用户在您的文本字段中输入多个 phone 数字,那么您需要提示用户使用逗号(或任何其他符号)分隔 phone 个数字,您可以通过逗号分隔字符串和将它添加到您的数组中,然后将该数组传递给 controller.recipients

这里有一个例子

if (MFMessageComposeViewController.canSendText())
        {
            let controller = MFMessageComposeViewController()
            controller.body = self.textView.text
            let phoneNumberString = "123456789,987654321,2233445566"
            let recipientsArray = phoneNumberString.components(separatedBy: ",")
            controller.recipients = recipientsArray
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
        else
        {
            print("Error")
        }

如果你想添加多个收件人那么你可以这样写...

这是我的代码...

if (MFMessageComposeViewController.canSendText()) 
{
    let composeVC = MFMessageComposeViewController()
    composeVC.messageComposeDelegate = self

    // Configure the fields of the interface.
    composeVC.recipients = ["4085551212","8597485365","2564756984"]
    composeVC.body = "Hello from California!"

    // Present the view controller modally.
    self.presentViewController(composeVC, animated: true, completion: nil)
}

func messageComposeViewController(controller: MFMessageComposeViewController,
                              didFinishWithResult result: MessageComposeResult)
{
    // Check the result or perform other tasks.

    // Dismiss the message compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}