如何从ios中的本机c代码调用javascript函数?

时间:2023-02-06 10:46:55

We are developing an iPhone app using phonegap, we are facing a small problem. We want to call a javascript function from native c code from appdelegate.m file.

我们正在使用phonegap开发iPhone应用程序,我们正面临一个小问题。我们想从appdelegate.m文件中的本机c代码调用javascript函数。

Following is our javascript function in test.js file

以下是我们在test.js文件中的javascript函数

function checkLink()
{  
    alert("Done..."); 
 }

I want to call the above function in appdelegate.m file in the below mentioned method

我想在下面提到的方法中调用appdelegate.m文件中的上述函数

- (void)applicationWillEnterForeground:(UIApplication *)application
   {
      [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];   
    }

Any help will be appreciable.

任何帮助都会很明显。

1 个解决方案

#1


0  

You need to use the JavaScriptCore framework that is bundled with iOS 7.

您需要使用与iOS 7捆绑在一起的JavaScriptCore框架。

First you must create a global JS context, and load your js functions to it:

首先,您必须创建一个全局JS上下文,并将js函数加载到它:

JSContext *context = [[JSContext alloc] init];  
[context evaluateScript:@"<testjsfiledata>"];

Then, you can call your functions as:

然后,您可以将您的功能称为:

context[@"checkLink"] = ^() {
    // objective-c stuff
};

#1


0  

You need to use the JavaScriptCore framework that is bundled with iOS 7.

您需要使用与iOS 7捆绑在一起的JavaScriptCore框架。

First you must create a global JS context, and load your js functions to it:

首先,您必须创建一个全局JS上下文,并将js函数加载到它:

JSContext *context = [[JSContext alloc] init];  
[context evaluateScript:@"<testjsfiledata>"];

Then, you can call your functions as:

然后,您可以将您的功能称为:

context[@"checkLink"] = ^() {
    // objective-c stuff
};