【问题标题】:Executing Javascript from Objective C in Cordova Plugin在 Cordova 插件中从 Objective C 执行 Javascript
【发布时间】:2017-10-06 07:15:53
【问题描述】:

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

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

例如在 AppDelegate.m 中,只是为了测试,我尝试了以下方法:

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

但我收到消息“'UIView' 的无可见 @inteface 声明选择器'stringByEvaluatingJavascriptFromString'”。

我创建了一个名为 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')"];


    }];

}

然后从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');"];

}

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

有什么建议吗?

谢谢 西梅翁

【问题讨论】:

    标签: javascript ios objective-c cordova


    【解决方案1】:

    使用evalJs 而不是stringByEvaluatingJavaScriptFromString

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多