Public API – iOS CREATE Application

The following example code demonstrates the creation of an application for a customer.

For the code below, the Customer API Key is stored within a singleton Configuration object.

/*
 *  Send the URL request with the required method for processing.
 */
+ (void)sendCreateApp
{
    NSString  *apiCreateRequest = @"https://api.bluedotinnovation.com/1/applications";
      
    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 createApp ] ];
  
    //  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) requiring an https connection
    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 application error:\n%@", jsonResponse );
                }
            }
        }
        else
        {
            NSLog( @"Create application: %@", error );
        }
    } ];
     
    //  Start the URL session task
    [ task resume ];
}
  
/*
 *  Create the JSON for a create application a literal name.
 *  Create each of the NSDictionaries that will be translated into the correct JSON format.
 */
+ (NSData *)createApp
{
    //  Create the application group
    NSDictionary  *content = [ NSDictionary dictionaryWithObjectsAndKeys:
                                        @"Demonstration app", @"name",
                                        @"au.com.bluedot.demonstration", @"packageName",
                                        @"01:00", @"nextRuleUpdateIntervalFormatted",
                                        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.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