我有一个包含多个UITextField的页面,用户可以在其中键入多个联系电话。单击发送按钮后,它应将预设的短信发送到列出的联系电话。我正在使用Twilio来运行此程序,并且正在使用功能功能,因此不必创建单独的服务器。我遇到的问题是,当列出多个号码时,它不会发送消息。我该如何解决这个问题,以便当用户键入多个数字时,它将向这些数字发送预设消息?
我已经尝试过多次修复它,但是它一直失败
这是我的快速代码:
@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
}
}
这是我的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
答案 0 :(得分:2)
此处是Twilio开发人员的传播者。
在<div class="wrap">
<div class="card">
<div class="front" onclick="getCard(0)"><img class="img1" src="https://lp.rustaro.ru/wp-content/uploads/transformation/cards/1.jpg" alt=""></div>
<div class="back"><img class="img1" src="" name="randimg1"></div>
</div>
<div class="card">
<div class="front" onclick="getCard(1)"><img class="img1" src="https://lp.rustaro.ru/wp-content/uploads/transformation/cards/1.jpg" alt=""></div>
<div class="back"><img class="img1" src="" name="randimg2"></div>
</div>
<div class="card">
<div class="front" onclick="getCard(2)"><img class="img1" src="https://lp.rustaro.ru/wp-content/uploads/transformation/cards/1.jpg" alt=""></div>
<div class="back"><img class="img1" src="" name="randimg3"></div>
</div>
</div>
函数中,我使用一个全局变量sendButton
从这样的文本框中创建一个电话号码数组:
numArray
然后在相同的numArray = [phonenumber.text!, phonenumber1.text!, phonenumber2.text!, phonenumber3.text!]
函数中,我将使用sendButton
向您的Twilio函数URL发送一个urlSession
请求。
POST
然后,您的Twilio函数应包含这样的代码,以遍历电话号码数组并向每个号码发送消息:
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()
希望这会有所帮助!