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

Check an SMS verification code with Twilio Verify

See more Twilio Examples

After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.

Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.

Twilio Verify API reference

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Obtain these credentials from the application's private configuration.
    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.basicAuth = true
    http.login = "YOUR_API_KEY_SID"
    http.password = "YOUR_API_KEY_SECRET"

    //  Both requests use the same Verify Service SID (VA...).
    let url = CkoStringBuilder()!
    url.append(value: "https://verify.twilio.com/v2/Services/")
    url.append(value: "YOUR_VERIFY_SERVICE_SID")
    url.append(value: "/VerificationCheck")

    let req = CkoHttpRequest()!
    req.httpVerb = "POST"
    req.contentType = "application/x-www-form-urlencoded"
    //  Load the saved SID for this user's pending attempt. Take only the code from user input.
    var verificationSid: String? = "SAVED_VERIFICATION_SID"
    var enteredCode: String? = "CODE_ENTERED_BY_USER"
    req.addParam(name: "VerificationSid", value: verificationSid)
    req.addParam(name: "Code", value: enteredCode)

    let resp = CkoHttpResponse()!
    success = http.httpReq(url: url.getAsString(), request: req, response: resp)
    if resp.statusCode.intValue == 0 {
        print("No response from Twilio. Check connectivity and TLS settings.")
        return
    }

    if resp.statusCode.intValue == 404 {
        print("No active verification found. Check the service and verification SIDs.")
        return
    }

    let result = CkoJsonObject()!
    var parsed: Bool = result.load(json: resp.bodyStr)
    if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
        print("Twilio returned HTTP \(resp.statusCode.intValue)")
        if (parsed == true) && (result.jsonType(of: "code").intValue == 2) {
            print("Twilio error code: \(result.int(of: "code").intValue)")
        }

        return
    }

    if (parsed != true) || (result.jsonType(of: "status").intValue != 1) {
        print("Verification status missing from the response.")
        return
    }

    var status: String? = result.string(of: "status")
    if status == "approved" {
        //  Record success for the user and phone number associated with verificationSid.
        print("Phone number verified.")
    }
    else {
        if status == "pending" {
            print("Code not accepted. Check the digits and enter the code again.")
        }
        else {
            print("Phone number unverified. Verification status: \(status!)")
        }

    }


}