Public API – iOS CREATE Custom Action

The following example code demonstrates the creation of a custom action that is assigned to a zone.

For the code below, the Customer API Key and Zone Id are stored within a singleton Configuration object.

/*
 *  Send the URL request with the required method for processing.
 */
+ (void)sendCustomActions
{
    NSString  *apiCreateRequest = @"https://api.bluedotinnovation.com/1/actions";
     
    NSURLSession *session = [ NSURLSession sharedSession ];
 
 
    //  Ensure that the command string is escaped properly for use in HTTP
    NSString  *escapedQuery = [ apiCreateRequest stringByAddingPercentEncodingWithAllowedCharacters: [ NSCharacterSet URLQueryAllowedCharacterSet ] ];
     
    NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL: [ NSURL URLWithString: escapedQuery ] ];
    [ request setHTTPMethod: @"POST" ];
    [ request setHTTPBody: [ self createCustomAction ] ];
 
    //  The content-type for the request must be explicitly provided when using JSON in the body
    [ request setValue: @"application/json" forHTTPHeaderField: @"Content-Type" ];
     
    //  Create an asynchronous connection; this will utilise App Transport Security (ATS) to an https connection is required
    NSURLSessionDataTask *task = [ session dataTaskWithRequest: request completionHandler: ^( NSData *data, NSURLResponse *response, NSError *error )
    {
        if ( error == nil )
        {
            if ( [ data length ] > 0 )
            {
                //  Retrieve the response as a dictionary containing the JSON
                NSDictionary *jsonResponse = [ NSJSONSerialization JSONObjectWithData: data options: 0 error: nil ];
                 
                //  Ensure that the response code from the Public API is for a successful transaction
                if ( ( ( NSString *)jsonResponse[ @"messageCode" ] ).intValue != 200 )
                {
                    NSLog( @"Create custom action error:\n%@", jsonResponse );
                }
            }
        }
        else
        {
            NSLog( @"Create custom action: %@", error );
        }
    } ];
    
    //  Start the URL session task
    [ task resume ];
}
 
/*
 *  Create the JSON for a create custom action request with a literal name.
 *  Create each of the NSDictionaries and NSArrays that will be translated into the correct JSON format.
 */
+ (NSData *)createCustomAction
{
    
    //  Create the custom action group
    NSDictionary  *customAction = [ NSDictionary dictionaryWithObjectsAndKeys:
                                        @"Triggered article in zone", @"name",
                                        nil];
     
    //  The custom actions are held within an array
    NSArray  *customActions = [ [ NSArray alloc ] initWithObjects: customAction, nil ];
    
    //  The array is stored within the custom actions group
    NSDictionary  *actions = [ NSDictionary dictionaryWithObjectsAndKeys:
                                  customActions, @"customActions",
                                  nil];
 
    /*
     *  The Zone Id for the custom action is stored within a singleton configuration object for this example.
     */
    NSDictionary  *zone = [ NSDictionary dictionaryWithObjectsAndKeys:
                           Configuration.instance.userZoneId, @"zoneId",
                           actions, @"actions",
                           nil ];
     
    //  Create the content group with the zone data
    NSDictionary  *content = [ NSDictionary dictionaryWithObjectsAndKeys:
                              zone, @"zone",
                              nil ];
     
    /*
     *  Create the security group.
     *  The Customer API Key is stored within a singleton configuration object for this example.
     */
    NSDictionary  *security = [ NSDictionary dictionaryWithObjectsAndKeys:
                               Configuration.instance.apiKey, @"apiKey",
                               Configuration.instance.customerApiKey, @"customerApiKey",
                               nil ];
    
    //  Create the request group with the data created above
    NSDictionary  *requestData = [ NSDictionary dictionaryWithObjectsAndKeys:
                                  security, @"security",
                                  content, @"content",
                                  nil ];
     
    //  Serialise the data above into a JSON data object
    NSError  *error;
    NSData  *jsonData = [ NSJSONSerialization dataWithJSONObject: requestData
                                                         options: NSJSONWritingPrettyPrinted
                                                           error: &error ];
     
    return( jsonData );
} 
Created by Bluedot DevOps on March 5, 2018

Start the discussion