【问题标题】:How to transfer a JSValueRef into a JavaScript var from Objective-C如何将 JSValueRef 从 Objective-C 传输到 JavaScript var
【发布时间】:2012-10-02 16:29:10
【问题描述】:

使用 JavaScriptCore,如果我在 Objective-C 中有这样的 NSString:

NSString *objcName = @"Kristof";

还有一个名为 jsContextRef 的 JSGlobalContextRef 上下文。

如何将 objcName 的 Objective-C 值转移到 jsContextRef 中的命名 JavaScript 变量中?我的想法是:

JSStringRef jsNameRef = JSStringCreateWithUTF8CString([objcName UTF8String]);
JSValueRef jsValueRef = JSValueMakeString(jsContextRef, jsNameRef);

假设变量名是“jsName”。我需要再打几个电话(甚至可能打一个电话),比如:

// This part is pseudo-code for which I would like to have proper code:
JSValueStoreInVarWithName(jsContextRef,"jsName",jsValueRef);

这样最终这个 JavaScript 在 Objective-C 中调用时会正确计算:

NSString *lJavaScriptScript = @"var jsUppercaseName = jsName.toUpperCase();";
JSStringRef scriptJS = JSStringCreateWithUTF8CString([lJavaScriptScript UTF8String]);
JSValueRef exception = NULL;
JSValueRef result = JSEvaluateScript(jsContextRef, scriptJS, NULL, NULL, 0, &exception);

【问题讨论】:

    标签: objective-c ios cocoa-touch javascriptcore


    【解决方案1】:

    我在sample code for JavaScriptCoreHeadstart,更具体地说是 JSWrappers.m 文件中找到了答案。它有这个方法:

    /* -addGlobalStringProperty:withValue: adds a string with the given name to the
     global object of the JavaScriptContext.  After this call, scripts running in
     the context will be able to access the string using the name. */
    - (void)addGlobalStringProperty:(NSString *)name withValue:(NSString *)theValue {
        /* convert the name to a JavaScript string */
        JSStringRef propertyName = [name jsStringValue];
        if ( propertyName != NULL ) {
            /* convert the property value into a JavaScript string */
            JSStringRef propertyValue = [theValue jsStringValue];
            if ( propertyValue != NULL ) {            
                /* copy the property value into the JavaScript context */
                JSValueRef valueInContext = JSValueMakeString( [self JSContext], propertyValue );
                if ( valueInContext != NULL ) {                
                    /* add the property into the context's global object */
                    JSObjectSetProperty( [self JSContext], JSContextGetGlobalObject( [self JSContext] ),
                                    propertyName, valueInContext, kJSPropertyAttributeReadOnly, NULL );
                }
                /* done with our reference to the property value */
                JSStringRelease( propertyValue );
            }
            /* done with our reference to the property name */
            JSStringRelease( propertyName );
        }
    }
    

    这正是我所需要的。 jsStringValue方法的代码在同一个项目的NSStringWrappers.m中,是:

    /* return a new JavaScriptCore string value for the string */
    - (JSStringRef)jsStringValue {
        return JSStringCreateWithCFString( (__bridge CFStringRef) self );
    }
    

    这似乎行得通。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多