Swift Requires Chilkat v11.0.0+
Swift
Schedule or cancel an SMS with Twilio
See more Twilio Examples
Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.
Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
let http = CkoHttp()!
http.requireSslCertVerify = true
http.basicAuth = true
http.login = "YOUR_API_KEY_SID"
http.password = "YOUR_API_KEY_SECRET"
let url = CkoStringBuilder()!
url.append(value: "https://api.twilio.com/2010-04-01/Accounts/")
url.append(value: "YOUR_ACCOUNT_SID")
url.append(value: "/Messages")
// Set this to true on a later run to cancel the saved message.
var cancelMessage: Bool = false
let req = CkoHttpRequest()!
let sendTime = CkoDateTime()!
req.httpVerb = "POST"
req.contentType = "application/x-www-form-urlencoded"
var expectedHttp: Int = 201
var expectedStatus: String? = "scheduled"
if cancelMessage != true {
var timeOk: Bool = sendTime.setFromCurrentSystemTime()
if timeOk != true {
print("\(sendTime.lastErrorText!)")
return
}
timeOk = sendTime.addSeconds(numSeconds: 3600)
if timeOk != true {
print("\(sendTime.lastErrorText!)")
return
}
req.addParam(name: "MessagingServiceSid", value: "YOUR_MESSAGING_SERVICE_SID")
req.addParam(name: "To", value: "RECIPIENT_E164_NUMBER")
req.addParam(name: "Body", value: "Scheduled message.")
req.addParam(name: "ScheduleType", value: "fixed")
req.addParam(name: "SendAt", value: sendTime.get(asTimestamp: false))
}
else {
// Update the individual message. This branch never creates another message.
url.append(value: "/")
url.append(value: "SAVED_SCHEDULED_MESSAGE_SID")
req.addParam(name: "Status", value: "canceled")
expectedHttp = 200
expectedStatus = "canceled"
}
url.append(value: ".json")
let resp = CkoHttpResponse()!
let reply = CkoJsonObject()!
success = http.httpReq(url: url.getAsString(), request: req, response: resp)
if resp.statusCode.intValue == 0 {
print("No HTTP response from Twilio.")
return
}
var parsed: Bool = reply.load(json: resp.bodyStr)
if (success != true) || (resp.statusCode.intValue != expectedHttp) {
print("Twilio returned HTTP \(resp.statusCode.intValue)")
if (parsed == true) && (reply.jsonType(of: "code").intValue == 2) {
print("Twilio error: \(reply.int(of: "code").intValue) \(reply.string(of: "message")!)")
}
return
}
if parsed != true {
print("Expected a JSON response from Twilio.")
return
}
if (reply.jsonType(of: "sid").intValue != 1) || (reply.jsonType(of: "status").intValue != 1) {
print("The response is missing the message SID or status.")
return
}
print("Message SID: \(reply.string(of: "sid")!)")
print("Current status: \(reply.string(of: "status")!)")
if reply.string(ofEquals: "status", value: expectedStatus, caseSensitive: true) != true {
print("The requested state was not confirmed. Check this SID before trying again.")
return
}
if cancelMessage != true {
print("Save the Message SID to check or cancel the scheduled message.")
}
}