Sample code for 30+ languages & platforms
Swift

Activix CRM Create a Phone

See more Activix CRM Examples

Create a phone. Returns the created phone.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    http.authToken = "ACCESS_TOKEN"

    http.accept = "application/json"

    // The following JSON is sent in the request body:

    // {
    //   "lead_id": 7135833,
    //   "number": "+15141234455",
    //   "type": "home"
    // }

    // Use this online tool to generate the code from sample JSON: 
    // Generate Code to Create JSON

    let jsonRequestBody = CkoJsonObject()!
    jsonRequestBody.updateInt(jsonPath: "lead_id", value: 7135833)
    jsonRequestBody.updateString(jsonPath: "number", value: "+15141234455")
    jsonRequestBody.updateString(jsonPath: "type", value: "home")

    var url: String? = "https://crm.activix.ca/api/v2/lead-phones"

    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: url, json: jsonRequestBody, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response Status Code: \(resp.statusCode.intValue)")

    let jsonResponse = CkoJsonObject()!
    jsonResponse.load(json: resp.bodyStr)
    jsonResponse.emitCompact = false
    print("\(jsonResponse.emit()!)")

    if resp.statusCode.intValue >= 300 {
        print("Failed.")
        return
    }

    // Sample output...
    // (See the parsing code below..)
    // 
    // Use the this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //     "data": {
    //         "id": 34566,
    //         "created_at": "2018-04-09T18:05:00+00:00",
    //         "updated_at": "2018-04-09T18:05:00+00:00",
    //         "lead_id": 3466512,
    //         "number": "+15141234455",
    //         ...
    //     }
    // }

    var dataId: Int
    var dataCreated_at: String?
    var dataUpdated_at: String?
    var dataLead_id: Int
    var dataExtension: String?
    var dataNumber: String?
    var dataType: String?
    var dataValid: Bool
    var dataValidated: String?
    var dataMobile: Bool

    dataId = jsonResponse.int(of: "data.id").intValue
    dataCreated_at = jsonResponse.string(of: "data.created_at")
    dataUpdated_at = jsonResponse.string(of: "data.updated_at")
    dataLead_id = jsonResponse.int(of: "data.lead_id").intValue
    dataExtension = jsonResponse.string(of: "data.extension")
    dataNumber = jsonResponse.string(of: "data.number")
    dataType = jsonResponse.string(of: "data.type")
    dataValid = jsonResponse.bool(of: "data.valid")
    dataValidated = jsonResponse.string(of: "data.validated")
    dataMobile = jsonResponse.bool(of: "data.mobile")

}