Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Check message delivery status with Twilio

See more Twilio Examples

Fetch an outbound SMS or MMS using the Message SID returned by a live send. A queued message is waiting to be sent. A sent message has been accepted by the upstream carrier. Delivered means Twilio received delivery confirmation. Some routes provide no final delivery receipt.

Read the current status once per lookup. For failed or undelivered messages, display the available delivery error code and text for troubleshooting. A missing message or an HTTP authentication error is a separate result from a delivery failure.

Twilio documentation

Chilkat Swift Downloads

Swift

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")
    url.append(value: "/")
    url.append(value: "YOUR_MESSAGE_SID")
    url.append(value: ".json")
    let resp = CkoHttpResponse()!
    let reply = CkoJsonObject()!

    success = http.httpNoBody(verb: "GET", url: url.getAsString(), 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 != 200) {
        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: "status").intValue != 1 {
        print("Message status missing from response.")
        return
    }

    print("Delivery status: \(reply.string(of: "status")!)")
    if (reply.string(ofEquals: "status", value: "failed", caseSensitive: true) == true) || (reply.string(ofEquals: "status", value: "undelivered", caseSensitive: true) == true) {
        if reply.jsonType(of: "error_code").intValue == 2 {
            print("Delivery error code: \(reply.int(of: "error_code").intValue)")
        }

        if reply.jsonType(of: "error_message").intValue == 1 {
            print("\(reply.string(of: "error_message")!)")
        }

    }


}