Sample code for 30+ languages & platforms
Swift

Twilio Send SMS (using Chilkat HTTP)

See more Twilio Examples

Send an outgoing SMS message.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // Implements the following CURL command:

    // (See information about using test credentials and phone numbers:  https://www.twilio.com/docs/iam/test-credentials)

    // curl -X POST https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json \
    // --data-urlencode "From=+15005550006" \
    // --data-urlencode "Body=body" \
    // --data-urlencode "To=+15005551212" \
    // -u TWILIO_ACCOUNT_SID:TWILIO_AUTH_TOKEN

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.login = "TWILIO_ACCOUNT_SID"
    http.password = "TWILIO_AUTH_TOKEN"

    let req = CkoHttpRequest()!
    req.httpVerb = "POST"
    req.path = "/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json"
    req.contentType = "application/x-www-form-urlencoded"
    req.addParam(name: "From", value: "+15005550006")
    req.addParam(name: "Body", value: "body")
    req.addParam(name: "To", value: "+15005551212")

    let resp = CkoHttpResponse()!
    success = http.httpReq(url: "https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json", request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    // A 201 status code indicates success.
    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    //   "api_version": "2010-04-01",
    //   "body": "body",
    //   "date_created": "Thu, 30 Jul 2015 20:12:31 +0000",
    //   "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",
    //   "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",
    //   "direction": "outbound-api",
    //   "error_code": null,
    //   "error_message": null,
    //   "from": "+15017122661",
    //   "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    //   "num_media": "0",
    //   "num_segments": "1",
    //   "price": null,
    //   "price_unit": null,
    //   "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    //   "status": "sent",
    //   "subresource_uris": {
    //     "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
    //   },
    //   "to": "+15558675310",
    //   "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var account_sid: String? = jResp.string(of: "account_sid")
    var api_version: String? = jResp.string(of: "api_version")
    var body: String? = jResp.string(of: "body")
    var date_created: String? = jResp.string(of: "date_created")
    var date_sent: String? = jResp.string(of: "date_sent")
    var date_updated: String? = jResp.string(of: "date_updated")
    var direction: String? = jResp.string(of: "direction")
    var error_code: String? = jResp.string(of: "error_code")
    var error_message: String? = jResp.string(of: "error_message")
    var from: String? = jResp.string(of: "from")
    var messaging_service_sid: String? = jResp.string(of: "messaging_service_sid")
    var num_media: String? = jResp.string(of: "num_media")
    var num_segments: String? = jResp.string(of: "num_segments")
    var price: String? = jResp.string(of: "price")
    var price_unit: String? = jResp.string(of: "price_unit")
    var sid: String? = jResp.string(of: "sid")
    var status: String? = jResp.string(of: "status")
    var subresource_urisMedia: String? = jResp.string(of: "subresource_uris.media")
    var v_to: String? = jResp.string(of: "to")
    var uri: String? = jResp.string(of: "uri")

}