Ios 如何通过Twilio功能向多个号码发送短信?

Ios 如何通过Twilio功能向多个号码发送短信?,ios,swift,sms,twilio,twilio-functions,Ios,Swift,Sms,Twilio,Twilio Functions,我有一个包含多个UITextFields的页面,用户可以在其中键入多个联系人号码。单击“发送”按钮时,应向列出的联系人号码发送预设文本消息。我正在使用Twilio来运行它,我正在使用功能特性,这样我就不必创建单独的服务器。我遇到的问题是,当列出多个号码时,它不会发送消息。我如何着手修复它,以便当用户键入几个数字时,它将向这些数字发送预设消息 我已经尝试过多次修复它,但总是失败 这是我的swift代码: @IBOutlet weak var phonenumber: UITextField

我有一个包含多个UITextFields的页面,用户可以在其中键入多个联系人号码。单击“发送”按钮时,应向列出的联系人号码发送预设文本消息。我正在使用Twilio来运行它,我正在使用功能特性,这样我就不必创建单独的服务器。我遇到的问题是,当列出多个号码时,它不会发送消息。我如何着手修复它,以便当用户键入几个数字时,它将向这些数字发送预设消息

我已经尝试过多次修复它,但总是失败

这是我的swift代码:

    @IBOutlet weak var phonenumber: UITextField!
    @IBOutlet weak var phonenumber1: UITextField!
    @IBOutlet weak var phonenumber2: UITextField!
    @IBOutlet weak var phonenumber3: UITextField!

    var currentTextField: UITextField?

    private let contactPicker = CNContactPickerViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        configureTextFields()
        configureTapGesture()

     }


    private func configureTextFields() {
        phonenumber.delegate = self
        phonenumber1.delegate = self
        phonenumber2.delegate = self
        phonenumber3.delegate = self

    }

    private func configureTapGesture(){
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap))
        viewcontact.addGestureRecognizer(tapGesture)

    }

    @objc private func handleTap(){
        viewcontact.endEditing(true)

    }

    @IBAction func sendbutton(_ sender: Any) {

        presentAlert(alertTitle: "", alertMessage: "Make sure all the contacts have a country code attached to it ie +60", lastAction: UIAlertAction(title: "Continue", style: .default) { [weak self] _ in



        let headers = [
            "Content-Type": "//urlencoded"
        ]


        let parameters: Parameters = [
            "To": self?.currentTextField?.text ?? "", // if "To": is set to just one text field ie "To": self?.phonenumber1.text ?? "", the sms is sent

            "Body": "Tester",


        ]

        Alamofire.request("//path", method: .post, parameters: parameters, headers: headers).response { response in
            print(response)

        }
        }
    )}

}

extension SelfTestTimer: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        currentTextField = nil
        textField.resignFirstResponder()
        return true
    }


    func textFieldDidBeginEditing(_ textField: UITextField) {


        if textField.hasText{
            //dont do anything

        }else{
        currentTextField = textField
        contactPicker.delegate = self
        self.present(contactPicker, animated: true, completion: nil)
        }
        return
    }


}
这是my Twilio函数中的代码:

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    const to = event.To;
    const body = event.Body;
    client.messages.create({
      from: 'Twilio Phone Number',
      to: to,
      body: body,

    }).then(msg => {
      callback(null);
    });

};

我希望它能够工作,这样它就可以向这里的
UITextFields

Twilio developer evangelist中列出的所有数字发送消息。 在
sendButton
函数中,我将使用全局变量
numaray
从如下文本框中创建一个电话号码数组:

numaray=[phonenumber.text!,phonenumber1.text!,phonenumber2.text!,phonenumber3.text!]

然后在同一个
sendButton
函数中,我将使用
urlSession
向您的Twilio函数URL发送
POST
请求

let Url = String(format: "REPLACE-WITH-YOUR-TWILIO-FUNCTION-URL")
        guard let serviceUrl = URL(string: Url) else { return }
        var request = URLRequest(url: serviceUrl)
        request.httpMethod = "POST"
        request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: numArray, options:[]) else {
            return
        }
        request.httpBody = httpBody

        let session = URLSession.shared
        session.dataTask(with: request) { (data, response, error) in
            if let response = response {
                print(response)
            }
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                    print("json ", json)
                } catch {
                    print(error)
                }
            }
        }.resume()
然后,您的Twilio函数应该包含这样的代码,以便在电话号码数组中循环,并向每个号码发送消息:

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    var nums = [event[0], event[1], event[2], event[3]]; //hardcoded for 4 textboxes
    nums.forEach(function(arrayNum) {
        client.messages.create({
            to: arrayNum,
            from: "REPLACE-WITH-YOUR-TWILIO-NUMBER",
            body: "REPLACE WITH YOUR MESSAGE/WHATEVER MESSAGE YOU WANT!"
        }).then(msg => {
            callback(null, msg.sid);
        }).catch(err => callback(err));
    });
};

希望这有帮助

它工作得非常好。非常感谢:)@lizziepika