Question

Utilisation de JavaScriptcore, si j'ai une nstring dans l'objectif-C comme ceci:

NSString *objcName = @"Kristof";

et un contexte jsglobalcontextref appelé jscontextref .

Comment transférer la valeur de l'objectif-c de l'objcname dans une variable JavaScript nommée dans le JSCONTEXTREF?Je pensais dans le sens de:

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

Disons que le nom de la variable sera "JSNAME".J'ai besoin de quelques appels supplémentaires (ou peut-être même un appel), quelque chose comme:

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

de sorte que, à la fin, ce JavaScript évaluera correctement quand appelé comme celui-ci dans l'objectif-C:

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

Était-ce utile?

La solution

J'ai trouvé la réponse dans le exemple de code pour javascriptcoreheadstart , plus spécifiquement le fichier jswrappers.m.Il a cette méthode:

/* -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 );
    }
}

Qui est exactement ce dont j'avais besoin.Le code de la méthode JSStringValue est dans NstringWrappers.m dans le même projet et est:

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

Cela semble fonctionner.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top