Developer Rajinikanth
Developer Rajinikanth

Reputation: 354

Worklight Adapter Call in IOS Native Passing JSON Parameter

When using JSON.parse(parameter-value) in JavaScript, the adapter invocation is working correctly, however when doing similarly in a native iOS app, it is failing with the following error.

Javascript Adapter Call:

var invocationData = {
    adapter : 'TEST_ADAP',
    procedure : 'PROC1',
    parameters : [JSON.parse(A)],
};

Native Call:

json= // some json value will be come
 MyConnect *connectListener = [[MyConnect alloc] initWithController:self];
    [[WLClient sharedInstance] wlConnectWithDelegate:connectListener];
    WLProcedureInvocationData *myInvocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"TEST" procedureName:@"test"];

    myInvocationData.parameters = [NSArray arrayWithObjects:json, nil];

    for (NSString *str in myInvocationData.parameters) {
        NSLog(@"values of account test %@",str);

    }
    PasswardPage *invokeListener = [[PasswardPage alloc] initWithController:self];
    [[WLClient sharedInstance] invokeProcedure:myInvocationData withDelegate:invokeListener];

Upvotes: 0

Views: 275

Answers (2)

Giriraj.Mulay
Giriraj.Mulay

Reputation: 53

We can pass JSON as NSString in iOS Native code to invoke adapter Example

//Created Dictionary 
NSMutablec *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"xyz" forKey:@"Name"];
[dict setObject:@"iOS" forKey:@"Platform"];

//Convert it to JSON Data
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:nil
                                                     error:&error];
//JSON Data To NSString

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

WLProcedureInvocationData * invocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"XYZAdapter" procedureName:@"FunctionXYZ"];

//Passing jsonString (NSString Created out of JSON Data) as array to set Parameters.

[invocationData setParameters:[NSArray arrayWithObject:jsonString]];

[[WLClient sharedInstance] invokeProcedure:invocationData withDelegate:self];

Upvotes: 0

Nathan H
Nathan H

Reputation: 49421

Your line

myInvocationData.parameters = [NSArray arrayWithObjects:json, nil];

is almost right.

The parameters property should be an NSArray (as you did) but the array must be made of string values - NOT a JSON object.

myInvocationData.parameters = [NSArray arrayWithObjects:@"myValue1", @"myValue2", @"myValue3", nil];

If the data you received is not in this format, you need to first convert it to this format. This is out of the scope of this question.

If you are not sure how to convert your existing format into a valid NSArray, please open a new question (tagged with Objective-C, not worklight).

Upvotes: 1

Related Questions