cordova-plugin-boilerplate icon indicating copy to clipboard operation
cordova-plugin-boilerplate copied to clipboard

update to swift 3

Open pablomaurer opened this issue 8 years ago • 4 comments

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 to yes to go ahead with Swift 2
  • what to do if I want to use Swift 3?

pablomaurer avatar Nov 24 '16 14:11 pablomaurer

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!') } }

hey-leon avatar Dec 28 '16 03:12 hey-leon

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.

pablomaurer avatar Feb 03 '17 11:02 pablomaurer

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.

mcfarljw avatar Dec 27 '17 02:12 mcfarljw

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);
  }
}

mibzman avatar Jan 12 '18 22:01 mibzman