Custom template with JSON object [iOS]

The following article describes how to create a custom template that receives a JSON string from the Leanplum Dashboard.

Steps:

  1. Place the following Leanplum defineAction definition before the Leanplum.start() call.

    For our example, we will name our new Action JSON_CONTENT. You can change this name by replacing the value of the NAME property inside the class. Keep in mind that this is the name that will also be displayed in Leanplum Dashboard. Inside the response listener, we are parsing the received JSON string to JSON object using NSJSONSerialization’s JSONObjectWithData method. For more info about the NSJSONSerialization’s class and its methods check out this iOS article. Inside the sample code, we will take a single String value with [data objectForKey:@"msg"] and displaying it in an alert.

     [Leanplum defineAction:@"JSON_CONTENT"

                                   ofKind: 1

                            withArguments:@[

                                            [LPActionArg argNamed:@"Message" withString:@"{}"]

                                            ]

                            withResponder:^BOOL(LPActionContext *context) {

                                @try {

                                    NSData *jsonData = [[context stringNamed:@"Message"] dataUsingEncoding:NSUTF8StringEncoding];

                                    NSError* error;

                                    NSDictionary* data = [ NSJSONSerialization

                                      JSONObjectWithData:jsonData

                                      options: kNilOptions

                                      error: &error ];

                                    if (NSClassFromString(@"UIAlertController")) {

                                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My new template" message:[data objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];

                                        UIAlertAction *accept = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                                           //action handler here

                                        }];

                                        [alert addAction:accept];

    //                                    [<your view controller here>

    //                                     presentViewController:alert animated:YES completion:nil];

                                    }
                                    return YES;
                                }

                                @catch (NSException *exception) {
                                    //exception handling here
                                    return NO;
                                }

                            }];
  2. Build the iOS application
  3. Register the new template in the Dashboard.
  4. Create a new message in the Dashboard with the new template and set a json string in the message body.  Example:
    Sample JSON:
    {"title":"My custom template","msg":"my custom message","okBtn":"ok","cancelBtn":"close", "likes":3, "rating":{"value":4}}

Final result:
Screen_Recording_2020-08-21_at_11.42.11_AM__1_.gif


Was this article helpful?
Have more questions? Submit a request