Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift (iOS) Plugin - Method not defined in Plugin Error - Cordova

I'm rewriting all plugins from an app from Objective-C to Swift. These plugins are called by Cordova. All the plugins work fine in Objective-C.

But when I try to run the app with the swift version plugin I got the following Error.

ERROR: Method 'getMAC2:' not defined in Plugin 'MACPlugin2'

I get this error in all method of any plugin that I rewrite to Swift.

Look this plugin code example:

import Foundation

@objc(MACPlugin2)
class MACPlugin2 : CDVPlugin {

    func getMAC2(command : CDVInvokedUrlCommand){

        print("########## ENTER MACPLUGIN 2 ###########")

    }


}

Someone have an idea of what are happening?

Best,

Flávio

like image 836
Flávio Leal Avatar asked Mar 09 '17 19:03

Flávio Leal


3 Answers

Adding the _ before the parameter was not enough for me. I had to add a @objc(test:) before the method as well.

@objc(test:)
func test(_ command: CDVInvokedUrlCommand) {
    // whatever
}
like image 141
Mathias Claassen Avatar answered Oct 29 '22 13:10

Mathias Claassen


My example was as following, without _ it was not working!! Notice: This solution is a fix for Swift 3

@objc(LibCDVP) class LibCDVP : CDVPlugin {
    func echo(_ command: CDVInvokedUrlCommand) {

        print("method call OK!")

        let msg = command.arguments[0] as? String ?? ""

        let pluginResult = CDVPluginResult(
            status: CDVCommandStatus_OK,
            messageAs: msg + ",ECHO"
        )

        self.commandDelegate!.send(
            pluginResult,
            callbackId: command.callbackId
        )
    }
}
like image 29
ossamacpp Avatar answered Oct 29 '22 11:10

ossamacpp


Dan, was right, I just added _ before the parameters.

Thanks.

like image 41
Flávio Leal Avatar answered Oct 29 '22 13:10

Flávio Leal