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

Send an MMS with Twilio using a media URL

See more Twilio Examples

Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.

Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.

Twilio Messages API reference

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.basicAuth = true
    //  Load the owning Account SID and an API key from application configuration.
    var accountSid: String? = "YOUR_ACCOUNT_SID"
    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: accountSid)
    url.append(value: "/Messages.json")

    //  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
    let req = CkoHttpRequest()!
    req.httpVerb = "POST"
    req.contentType = "application/x-www-form-urlencoded"
    req.addParam(name: "From", value: "YOUR_MMS_ENABLED_NUMBER")
    req.addParam(name: "To", value: "RECIPIENT_E164_NUMBER")
    //  Supply a URL for the file itself so Twilio can retrieve the media.
    req.addParam(name: "MediaUrl", value: "https://media.example.com/photo.jpg")
    req.addParam(name: "Body", value: "Photo attached.")

    let resp = CkoHttpResponse()!
    let reply = CkoJsonObject()!
    success = http.httpReq(url: url.getAsString(), request: req, response: resp)
    if resp.statusCode.intValue == 0 {
        print("\(http.lastErrorText!)")
        return
    }

    var parsed: Bool = reply.load(json: resp.bodyStr)
    if (success != true) || (resp.statusCode.intValue != 201) {
        print("HTTP \(resp.statusCode.intValue)")
        if (parsed == true) && (reply.hasMember(jsonPath: "code") == true) {
            print("Twilio error \(reply.int(of: "code").intValue): \(reply.string(of: "message")!)")
        }

        return
    }

    if parsed != true {
        print("Expected a JSON response.")
        return
    }

    if (reply.jsonType(of: "sid").intValue != 1) || (reply.jsonType(of: "status").intValue != 1) {
        print("The create response is missing the message SID or status.")
        return
    }

    print("Message SID: \(reply.string(of: "sid")!)")
    print("Initial status: \(reply.string(of: "status")!)")

}