在Cordova插件中从Objective C执行Javascript

时间:2022-09-12 20:28:35

I'm using cordova 6.5.0 and I'm creating a plugin from which I would like to execute some javascript.

我正在使用cordova 6.5.0并且我正在创建一个插件,我想从中执行一些javascript。

I have found in forums that from my webview I can use stringByEvaluatingJavascriptFromString but it can not be recognised as a valid method.

我在论坛中发现,从我的webview我可以使用stringByEvaluatingJavascriptFromString但它不能被识别为有效的方法。

For instance in AppDelegate.m, just for testing, I've tried the following:

例如在AppDelegate.m中,仅用于测试,我尝试了以下内容:

[self.viewController.webView stringByEvaluatingJavascriptFromString]:@"alert('Test')"];

but I receive the message "No visible @inteface for 'UIView' declares the selector 'stringByEvaluatingJavascriptFromString'".

但我收到消息“没有可见@inteface for'UIView'声明了选择器'stringByEvaluatingJavascriptFromString'”。

I have created a class object named Utils.m

我创建了一个名为Utils.m的类对象

@implementation Utils: NSObject

id webview;
id delegate;


-(void) initialise:(id) wbview Delegate:(id) dlg{

    webview = wbview;
    delegate = dlg;

}

-(void) executeJavascript:(NSString *)str{

    [delegate runInBackground:^{

        [delegate stringByEvaluatingJavaScriptFromString:@"alert('test')"];


    }];

}

And then from the cordova plugin from pluginInitialize I have

然后从pluginInitialize的cordova插件中获取

- (void)pluginInitialize{


    /* Start pjsua app thread */
//    [NSThread detachNewThreadSelector:@selector(pjsuaStart) toTarget:self withObject:nil];

    utils = [[Utils alloc] init ];
    [utils initialise:self.webView Delegate:self.commandDelegate];

    [utils executeJavascript:@"alert('Test');"];

}

Although for some reason the stringByEvaluatingJavascriptFromString is considered a valid method, the application crashes...

虽然由于某种原因stringByEvaluatingJavascriptFromString被认为是一个有效的方法,但应用程序崩溃...

Any suggestions?

有什么建议么?

Thanks Symeon

谢谢Symeon

1 个解决方案

#1


1  

Use evalJs not stringByEvaluatingJavaScriptFromString:

使用evalJs而不是stringByEvaluatingJavaScriptFromString:

[delegate evalJs:@"alert('test')"];

#1


1  

Use evalJs not stringByEvaluatingJavaScriptFromString:

使用evalJs而不是stringByEvaluatingJavaScriptFromString:

[delegate evalJs:@"alert('test')"];