本文介绍了如何编写Cordova插件在Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将现有的自订外挂程式转换为Swift语言:

I converted existing custom plugin to Swift language:

(位于 Plugins / CustomPluginInSwift.swift

import Foundation

class CustomPluginInSwift : CDVPlugin {

    func getSettings(command: CDVInvokedUrlCommand) {

        println("CustomPluginInSwift :: getSettings is called")

        var pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
        commandDelegate.sendPluginResult(pluginResult, callbackId:command.callbackId)
    }
}

我有两个问题:


  • 未找到CDVPlugin
  • Javascript看不到插件: CustomPluginInSwift

  • CDVPlugin not found
  • Javascript doesn't see plugin: CustomPluginInSwift:

CDVPlugin类CustomPluginInSwift(pluginName:CustomPluginInSwift)不存在

我留下 config.xml

I left config.xml the same but it doesn't work as expected.

我的问题在哪里?

推荐答案

如上所述,您必须添加一个bridging-header.h文件,其中包含

As is mentioned you have to add a bridging-header.h file which contains

#import <Cordova/CDV.h>

还需要在XCode项目属性中添加桥接头的路径 - > Build Settings-> Objective- C桥接接头。例如:

Also you need to add the bridging header's path in XCode project properties->Build Settings->Objective-C Bridging Header. For example:

your-app-name/plugins/com.plugin.example/bridging-header.h

此外,为了让Objective-C看到相同的插件类名,您需要添加一个@objc映射类声明。它可以与swift类名称本身相同,或者不同的东西。在这个例子中,HWPCustomPluginInSwift将是什么Objective-C(和Javascript)将最终看到:

Additionally, in order for Objective-C to see the same plugin class name, you need to add an @objc mapping to the class declaration. It can be the same as the swift class name itself, or something different. In this example, "HWPCustomPluginInSwift" will be what Objective-C (and Javascript) will end up seeing:

@objc(HWPCustomPluginInSwift) class CustomPluginInSwift : CDVPlugin {

,然后config.xml文件中的feature节点应该如下所示:

and then your feature node in config.xml file should look like this:

<feature name="CustomPluginInSwift">
    <param name="ios-package" value="HWPCustomPluginInSwift" />
</feature>

这篇关于如何编写Cordova插件在Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 13:23