简体   繁体   中英

How do I pass a swift object to javascript (UIWebView / objective-C)

I am working on passing data from OBJECTIVE-C to javascript inside a UIWebView

I have a custom Class as Item and i need to send object of item to javascript at server end.

for passing the variables i have used:

NSString *runScript=[NSString stringWithFormat:@"javascript:setData(%ld,%@)",(long)self.subItemId,self.itemId];

Thanks in advance

I am using myVar as a global variable, however I don't believe this is needed in most cases.

+ (BNRContact *)editContact:(BNRContact *)aContact{
    //create script
    NSString *jsFunctionText =
    @"myVar=new Object();"
    "var contactObject = function(obj){"
    "myVar = {obj}";
    "myVar.name = 'April';"
    "return myVar;"
    "}";
    //set up script context
    JSContext *context = [JSContext new];
    context[ @"myVar" ] = aContact;
    [context evaluateScript:jsFunctionText];

    // calling a JavaScript function
    JSValue *jsFunction = context[@"contactObject"];
    JSValue *value = [jsFunction callWithArguments:@[ aContact ]];

    aContact = [value toObject];
    return aContact;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM