akofman / cordova-plugin-boilerplate

iOS Cordova plugin boilerplate written in Swift.
MIT License
16 stars 11 forks source link

update to swift 3 #4

Open pablomaurer opened 7 years ago

pablomaurer commented 7 years ago

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:

hey-leon commented 7 years ago

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

pablomaurer commented 7 years ago

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.

mcfarljw commented 6 years ago

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.

mibzman commented 6 years ago

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