Public API – iOS CREATE Zone

The following code demonstrates how to create a Zone using the Public API in an iOS app using a synchronous URL call; NSURLConnection is utilised for this particular example, the other sample code uses NSURLSessionDataTask as NSURLConnection has been deprecated in iOS9 and does not interact with the App Transport Security.

For the code below, the App API Key, 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)sendCreateZone
{
    NSString  *apiCreateRequest = @"https://api.bluedotinnovation.com/1/zones";
 
    //  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 createUserZone ] ];
 
    //  The content-type for the request must be explicitly provided when using JSON in the body
    [ request setValue: @"application/json" forHTTPHeaderField: @"Content-Type" ];
    
    //  Create the Zone using a synchronous URL connection; this is only utilised to cover the different URL connections that may be utilised
    NSURLResponse  *response;
    NSError  *error;
    NSData  *data = [ NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &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 )
            {
                Configuration.instance.userZoneId = [ jsonResponse[ @"zoneId" ] copy ];
            }
            else
            {
                NSLog( @"Create zone error:\n%@", jsonResponse );
            }
        }
    }
    else
    {
        NSLog( @"Create Zone error: %@", error );
    }
}
 
/*
 *  Create the JSON for a create Zone request.
 */
+ (NSData *)createUserZone
{
    //  Create the time from group, start at a minute past midnight
    NSDictionary  *timeFrom = [ NSDictionary dictionaryWithObjectsAndKeys:
                               @"00:01", @"time",
                               @"am", @"period",
                               nil ];
     
    //  Create the time to group, ending at a minute to midnight
    NSDictionary  *timeTo = [ NSDictionary dictionaryWithObjectsAndKeys:
                             @"11:59", @"time",
                             @"pm", @"period",
                             nil ];
    
    //  Assign the times to the time active group
    NSDictionary  *timeActive = [ NSDictionary dictionaryWithObjectsAndKeys:
                                 timeFrom, @"from",
                                 timeTo, @"to",
                                 nil ];
    /*
     *  Create the Zone group; with Zone check out enabled.
     *  The Zone Id is stored within a singleton configuration object for this example.
     */
    NSDictionary  *zone = [ NSDictionary dictionaryWithObjectsAndKeys:
                           Configuration.instance.userZoneName, @"zoneName",
                           [ NSNumber numberWithBool: YES ], @"enableCheckOut",
                           @"00:05", @"minimumRetriggerTime",
                           timeActive, @"timeActive",
                           nil ];
 
    //  Create the content group with the Zone data
    NSDictionary  *content = [ NSDictionary dictionaryWithObjectsAndKeys:
                              zone, @"zone",
                              nil ];
 
    /*
     *  Create the security group.
     *  The app API Key and Customer API Key are stored within a singleton configuration object for this example.
     */   
    NSDictionary  *security = [ NSDictionary dictionaryWithObjectsAndKeys:
                               Configuration.instance.apiKey, @"apiKey",
                               Configuration.instance.customerApiKey, @"customerApiKey",
                               nil ];
 
     
    NSDictionary  *requestData = [ NSDictionary dictionaryWithObjectsAndKeys:
                                  security, @"security",
                                  content, @"content",
                                  nil ];
     
    //  Create the request group with the data created above
    NSError  *error;
    NSData  *jsonData = [ NSJSONSerialization dataWithJSONObject: requestData
                                                         options: 0
                                                           error: &error ];
     
    return( jsonData );
} 
Created by Bluedot DevOps on March 5, 2018

Start the discussion