I have a page with multiple UITextFields where a user can type in multiple contact numbers. When the send button is clicked it should send a preset text message to the contact numbers listed. I'm using Twilio to run this and I'm using the functions feature so that I don't have to create a separate server. The issue I'm having is that it doesn't send the message out when there is more than one number listed. How do I go about fixing it so that when a user keys in several numbers, it will send the preset message to those said numbers?
I have tried multiple times to fix it but it keeps failing
This is my code in 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
}
}
This is the code in my Twilio function:
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);
});
};
I would like it to work so that it sends a message to all the numbers listed in the UITextFields