How to call JavaScript Function in objective C

后端 未结 5 1696
借酒劲吻你
借酒劲吻你 2020-12-01 04:09

I am new programmer to objective C. I added some html file to Objective C. In this html file contains simple javascript function. I need to call that Function through thw ob

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 04:15

    One can use JavaScriptCore framework to run JavaScript code from Objective-C.

    #import 
    
    ...
    
    JSContext *context = [[JSContext alloc] init];
    [context evaluateScript: @"function greet(name){ return 'Hello, ' + name; }"];
    JSValue *function = context[@"greet"];
    JSValue* result = [function callWithArguments:@[@"World"]];
    [result toString]; // -> Hello, World
    

    Here is a demo:

    https://github.com/evgenyneu/ios-javascriptcore-demo

提交回复
热议问题