加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Swift中编写Cordova插件?

发布时间:2020-12-14 05:52:19 所属栏目:百科 来源:网络整理
导读:我将现有的自定义插件转换为Swift语言: (位于Plugins / CustomPluginInSwift.swift下) import Foundationclass CustomPluginInSwift : CDVPlugin { func getSettings(command: CDVInvokedUrlCommand) { println("CustomPluginInSwift :: getSettings is cal
我将现有的自定义插件转换为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项目属性 – >构建设置 – > Objective-C桥接头中添加桥接头的路径。例如:

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读