2

I'm using WebScripting resp. WebScriptObject to have communicate from my WebApp to an ObjC interface and vice-versa.

I can also call methods on JS objects. This works great. Any returned object is converted to ObjC types like NSArray or NSDictionary.

Now I want to SEND an object. In JavaScript I would do it like this:

external.processDict({a: 1, b: 2, c: 3});

But how can I do that using ObjC? The docs say:

/*!
    @method callWebScriptMethod:withArguments:
    @param name The name of the method to call in the script environment.
    @param arguments The arguments to pass to the script environment.
    @discussion Calls the specified method in the script environment using the specified arguments.
    @result Returns the result of calling the script method.
    Returns WebUndefined when an exception is thrown in the script environment.
*/
- (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)arguments;

This does not help at all. The arguments is an array of "arguments to pass to the script environment". The Documentation says "The values to pass to the method".

As WHAT?! Apples? Bananas? NSObject or what kind of whatever?

When I pass a dictionary (for example the one that I received using WebScripting protocol) then I get an empty object of type ObjCRuntimeObject with no values and no keys.

So how do I send arrays and/or dictionaries to the environment?

What I tried so far:

+ (id)_parseObject:(id)object {
    if ([object isKindOfClass:[NSArray class]]) {
        return [self webScriptObjectForArray:object];
    }
    else if ([object isKindOfClass:[NSDictionary class]]) {
        return [self webScriptObjectForDctionary:object];
    }
    else if ([object respondsToSelector:@selector(stringValue)]) {
        return [object stringValue];
    }

    return [NSString stringWithFormat:@"%@", object];
}

+ (WebScriptObject *)webScriptObjectForDctionary:(NSDictionary *)dictionary {
    dictionary = [dictionary copy];
    WebScriptObject *object = [[WebScriptObject alloc] init];

    for (NSString *key in [dictionary allKeys]) {
        [object setValue:[self _parseObject:[dictionary objectForKey:key]] forKey:key];
    }

    return object;
}

+ (WebScriptObject *)webScriptObjectForArray:(NSArray *)array {
    array = [array copy];
    WebScriptObject *object = [[WebScriptObject alloc] init];

    for (int i = 0; i < [array count]; i++) {
        [object setValue:[self _parseObject:[array objectAtIndex:i]] forKey:[NSString stringWithFormat:@"%@", @(i)]];
    }

    return object;
}

But I cannot set object values of that WebScriptObject, I'm always getting EXC_BAD_ACCESS.

EDIT I

I discovered using NSArray I get an array. So how working with dictionaries?

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107

0 Answers0