Cloud Code with Swift Apps

In this guide, we will show how work with the Parse’s Cloud Code and how create functions to work with it.

 

Step 1: Create a JavaScript file called main.js:

Put the functions that you want to execute with Cloud Code in this file. For create a Cloud Code function, you have to define a new function using the Parse.Cloud.define function:

Parse.Cloud.define(“functionName”, function (request, response) {
    // function code
});

If you need to create a lot of Cloud Code functions, you can separate this functions in multiples files and just reference the files in main.js:

// Reference to service 1
var service1 = require(“./path/to/service1.js”);
// Reference to service 2
var service2 = require(“./path/to/service2.js”);

Step 2: Upload the JavaScript files to back4app cloud:

Go to your app dashboard, click on “Cloud Code”:

back4appdashboard

Next, click on “Choose file”, select the .js file, then click on “Save”:cloudcode

Step 3: Call the function from the app or a REST API:

To call the function from the app, we need to use the PFCloud.callFunctionInBackground method, using the name of the desire function in the first argument:

PFCloud.callFunctionInBackground(“functionName”, withParameters: [desireParameters]) { (response, error) in
    if error == nil {
        // The function was successfully executed and you have a correct // response object
    } else {
       // The function returned a error
    }
}

To call a cloud code function from a REST API, use the code below:

curl -X POST -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-H "Content-Type: application/json" \
-d ‘{ // Put the function parameters here in JSON format }’ \
https://parseapi.back4app.com/functions/functionName