objective-cjavascriptcore

How to check if a JSValue is a function


I am using JavaScriptCore to interact with Objective-C code

There is no isFunction method in JSValue, but I want to check if a JSValue is a JS Function and call it from Objective-C


Solution

  • I ended up inject a small javascript code to return the result of typeof

    [context evaluateScript:@"function is(obj){ return typeof obj }"];
    JSValue *is = [[context globalObject] valueForProperty:@"is"];
    JSValue *type = [is callWithArguments:@[ is ]];