cordova - 如何在 Swift 中编写 Cordova 插件?

标签 cordova swift

我将现有的自定义插件转换为 Swift 语言:

(位于 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 class CustomPluginInSwift (pluginName: CustomPluginInSwift) does not exist

我保留了 config.xml,但它没有按预期工作。

我的问题在哪里?

最佳答案

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

#import <Cordova/CDV.h>

您还需要在 XCode 项目属性->build设置->Objective-C 桥接 header 中添加桥接 header 的路径。例如:

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

此外,为了让 Objective-C 看到相同的插件类名,您需要将 @objc 映射添加到类声明中。它可以与 swift 类名本身相同,也可以不同。在此示例中,“HWPCustomPluginInSwift”将是 Objective-C(和 Javascript)最终看到的内容:

@objc(HWPCustomPluginInSwift) class CustomPluginInSwift : CDVPlugin {

然后 config.xml 文件中的功能节点应如下所示:

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

关于cordova - 如何在 Swift 中编写 Cordova 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448976/

相关文章:

ios - 动态添加自定义 UITableViewCell

Swift 使用 nil 合并运算符简化可选 Int 到 String 的转换

javascript - 布局phonegap

angularjs - 如何使用 angularjs 和谷歌地图更改图标标记

cordova - npm WARN 已弃用 minimatch@2.0.10 : Please update to minimatch 3. 0.2 或更高版本以避免 RegExp DoS 问题

cordova - 错误 : could not find gradle wrapper within android sdk. 可能需要更新您的 android sdk

ios - 在 Swift 中编写一个永远循环的动画

ios - 铿锵: error: no such file or directory: 'ObjC' cannot compile my app anymore

swift - 如何在 Swift 中将图像放入 NSCache?

swift - Framework Pod 说库未加载