cordova-plugin-boilerplate
cordova-plugin-boilerplate copied to clipboard
update to swift 3
I ran
cordova plugin add cordova-plugin-add-swift-support --save
cordova plugin add https://github.com/akofman/cordova-plugin-boilerplate --save
added Test Code:
cordova.plugins.yourPluginName.sayHello(
'huga-huga ',
function(msg) {
console.log('worked', msg);
},
function(err) {
console.log('failed', err);
}
);
Got Build Errors and probably because of Swift 3, Xcode suggested some changes i applied but still works not.
import Foundation
@objc(YourPluginNamePlugin) class YourPluginNamePlugin : CDVPlugin {
func sayHello(command: CDVInvokedUrlCommand) {
let message = "Hello !";
//XCODE: messageAsString renamed to messageAs
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message);
//XCODE: sendPluginResult renamed to send
commandDelegate.send(pluginResult, callbackId:command.callbackId);
}
}
Getting Error
ERROR: Method 'sayHello:' not defined in Plugin 'YourPluginName'
my progress:
- set
Use Legacy Swift Language Version
toyes
to go ahead with Swift 2 - what to do if I want to use Swift 3?
Have you tryed adding @objc() annotations to your classes methods? I have resolved issues with this before.
e.g.
@objc(helloworld) class helloworld : CDVPlugin { @objc(hi) func hi (cmd: CDVInvokedUrlCommand) { print('Hi!') } }
Using @obj-c
in methods gives error @objc method name provides names for 0 arguments, but method has one parameter
Also searched for sample projects but all seem to use swift 2.
For swift 3 you'll need to change the function to the follow:
func sayHello(_ command: CDVInvokedUrlCommand)
It was frustrating to find the solution, but just adding _
makes everything magically work.
I was able to fix the issues with swift 3 by changing yourpluginname.swift to contain the following:
import Foundation
@objc(YourPluginNamePlugin) class YourPluginNamePlugin : CDVPlugin {
func sayHello(command: CDVInvokedUrlCommand) {
let message = "Hello !";
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message);
commandDelegate.send(pluginResult, callbackId:command.callbackId);
}
}