Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Cordova调用Swift函数_Javascript_Ios_Cordova_Swift3_Cordova Plugins - Fatal编程技术网

Javascript 从Cordova调用Swift函数

Javascript 从Cordova调用Swift函数,javascript,ios,cordova,swift3,cordova-plugins,Javascript,Ios,Cordova,Swift3,Cordova Plugins,我有一个用Swift编写的本机iOS应用程序,它可以独立运行。然而,目前,我正在探索Cordova,并使用我已有的Swift代码为其开发一个插件。假设我的本机Swift代码如下,我要调用的函数是startDataLog(): func startDataLog() { // Set file logging parameters and do some logging etc... let fileName = "log_\(dateFormatter.string(from: Date

我有一个用Swift编写的本机iOS应用程序,它可以独立运行。然而,目前,我正在探索Cordova,并使用我已有的Swift代码为其开发一个插件。假设我的本机Swift代码如下,我要调用的函数是startDataLog()

func startDataLog() {
  // Set file logging parameters and do some logging etc...
  let fileName = "log_\(dateFormatter.string(from: Date())).log"
  let filePath = URL(fileURLWithPath: 
  (NSSearchPathForDirectoriesInDomains(.documentDirectory, 
  .userDomainMask, true)[0] as 
  NSString).appendingPathComponent(fileName))
  //do other stuffs here...
 }
假设为新插件正确配置了plugin.xml和桥接头: 首先,我需要在www/plugin.js中添加这个调用iOS代码的代码。就我而言,startDataLog:

var exec = require('cordova/exec');
var serviceName = 'DriverAppPlugin'

module.exports = {
  'startDataLog': function (successCallback, errorCallback) {
   cordova.exec(successCallback, errorCallback, serviceName, 
   'startDataLog', [])
    },
  }
然后,在我的本地Swift代码中,我必须添加以下内容:

@objc(CentralPlugin) class CentralPlugin: CDVPlugin {
 func startDataLog(command: CDVInvokedUrlCommand) {
   // Set file logging parameters and does some extras
   let fileName = "log_\(dateFormatter.string(from: Date())).log"
   let filePath = URL(fileURLWithPath: 
   (NSSearchPathForDirectoriesInDomains(.documentDirectory, 
   .userDomainMask, true)[0] as 
   NSString).appendingPathComponent(fileName))
   //do other stuffs here...
     }
   }     
我不清楚的是,是否需要对本机Swift代码或我要调用的函数执行此操作?正在添加

(command: CDVInvokedUrlCommand)
对func的解释是正确的,或者我在这里犯了严重的错误。我是Cordova的新手,事实上,基于集成Cordova+Swift的教程并不多


任何反馈或指导都会对启动插件开发非常有帮助。请建议。

我现在也有同样的情况。。!任何人都能找到从javascript调用swift view controller函数的解决方案吗。。请告诉任何人。