我想使用 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)}
}
}
任何建议如何将其发送给多个收件人?
答案 0 :(得分:4)
如果用户在文本字段中输入多个电话号码,则需要提示用户使用逗号(或任何其他符号)分隔电话号码,您可以通过逗号分隔字符串并将其添加到数组中,然后传递该数组在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")
}
答案 1 :(得分:1)
如果您想添加多个收件人,那么您可以写这个......
这是我的代码......
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)
}