Executing server code via the Kii Cloud SDK for iOS
Executing server code via the Kii Cloud SDK for iOS
Here is an example of executing server code via the iOS SDK.
Swift:
Objective-C:
Here is the brief explanation of the sample code:
Creates a KiiServerCodeEntry instance by executing the serverCodeEntry(_:) method with the target endpoint (function) name. You can specify the version of the JavaScript engine for executing the server code with the environmentVersion argument.
Prepares custom parameters (the username and password in this sample code) by setting them in an NSDictionary and by creating a KiiServerCodeEntryArgument with the KiiServerCodeEntryArgument(dictionary:) method. Make sure to align the parameter keys with those in the server code so that the server code can properly get the parameter values (in this sample code the keys are "username" and "password").
Executes the server code by calling the execute(_:) method with the custom parameters.
If there is a logged-in user, Kii Cloud interprets the server code as being executed by this user.
If there is no logged-in user, Kii Cloud interprets the server code as being executed by an anonymous user.
Gets the result by calling the returnedValue() method.
(Optionally) gets the number of the executed steps with the executedSteps() method.
(Optionally) gets the version of Node.js with the environmentVersion() method.
The returnedValue() method will give you an NSDictionary with the result stored in the "returnedValue" key (See Data types of return values for some examples). Use the appropriate method to fetch the actual result.
If your server code returns the value after the timeout, you can get the value with the returnedValue() method likewise. See Timeout for the format of the NSDictionary you can get with the method.