-1

I’m trying to create a custom iOS plugin for an app built with Ionic + Capacitor 7. I migrated from Capacitor 5 to Capacitor 7.

Sources:

https://capacitorjs.com/docs/ios/custom-code#register-the-plugin

"App" Plugin Not Implemented on iOS After Upgrading Capacitor to Version 6

I followed the official documentation and created the Swift files inside ios/App/App/Sources/EchoPlugin/:

Echo.swift

import Foundation

@objc public class Echo: NSObject {
    @objc public func echo(_ value: String) -> String {
        print(value)
        return value
    }
}

EchoPlugin.swift

import Foundation
import Capacitor

@objc(EchoPlugin)
public class EchoPlugin: CAPPlugin, CAPBridgedPlugin {
    public let identifier = "EchoPlugin"
    public let jsName = "Echo"
    public let pluginMethods: [CAPPluginMethod] = [
        CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise)
    ]

    private let implementation = Echo()

    @objc func echo(_ call: CAPPluginCall) {
        let value = call.getString("value") ?? ""
        call.resolve([
            "value": implementation.echo(value)
        ])
    }
}

MainViewController.swift

import UIKit
import Capacitor

class MainViewController: CAPBridgeViewController {
    override open func capacitorDidLoad() {
        bridge?.registerPluginInstance(EchoPlugin())
    }
}

And on the TypeScript side (src/app/plugins/EchoPlugin/index.ts):

import { registerPlugin } from '@capacitor/core';

export interface EchoPlugin {
  echo(options: { value: string }): Promise<{ value: string }>;
}

const Echo = registerPlugin<EchoPlugin>('Echo');

export default Echo;

When I call it from my app:

try {
      console.log('--- Testing Echo Plugin ---');
      
      const { value } = await Echo.echo({ value: 'Hello World!' });
      console.log('Response from native:', value);
    } catch (error) {
      console.error('Error calling Echo plugin:', error);
      
    }

Echo plugin is not implemented on ios

Is there any additional step required in Capacitor 7 for a local iOS plugin to be detected? Were there changes in how plugins are manually registered between Capacitor 5 and 7?

I have modified my MainViewController, but it never launches, so I cannot see this message.

import UIKit import Capacitor

class MainViewController: CAPBridgeViewController {

override open func capacitorDidLoad() {
  print(">>> EchoPlugin registrado en capacitorDidLoad")
  bridge?.registerPluginInstance(EchoPlugin().self)
  print(">>> EchoPlugin registrado en capacitorDidLoad")
}

}

If tried in Appdelegate: bridge = CAPBridge(self.window!) print("AppDelegate: didFinishLaunchingWithOptions")

    CAPBridge.registerPlugin(EchoPlugin.self) 

Show me 2 errors

Argument passed to call that takes no arguments

Type 'CAPBridge' has no member 'registerPlugin'

2 Answers 2

0

In Capacitor 7 the way local plugins are registered on iOS changed.
Just creating the Swift files and calling bridge?.registerPluginInstance() won’t work anymore.

You need to do following steps:
Make sure your Swift plugin uses the same name in both Swift and TS:

public let identifier = "Echo" 
public let jsName = "Echo" 

and in TS
const Echo = registerPlugin<EchoPlugin>('Echo');

2.Register the plugin in CAPBridge.registerPlugin(EchoPlugin.self)

3. Run npx cap sync ios and rebuild in Xcode.

This should work. Let me know if you need help in this.
--

EDIT 1:

import Capacitor

@objc(EchoPlugin)
public class EchoPlugin: CAPPlugin, CAPBridgedPlugin {
    public let identifier = "EchoPlugin"
    public let jsName = "Echo"
    public let pluginMethods: [CAPPluginMethod] = [
        CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise)
    ]

    private let implementation = Echo()

    @objc func echo(_ call: CAPPluginCall) {
        let value = call.getString("value") ?? ""
        call.resolve([
            "value": implementation.echo(value)
        ])
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

English: I’ve tested the changes you suggested and, if I understood correctly, it would mean modifying this file, but I don’t know why this file never gets modified, it never gets called, I don’t understand. It’s supposed to be included in Xcode and it should work. // MainViewController root the project print(">>> EchoPlugin registrado en capacitorDidLoad") bridge?.registerPluginInstance(EchoPlugin().self)
it makes sense that you don’t see MainViewController getting called. In recent Capacitor versions, plugin registration moved out of MainViewController. That’s why your print(">>> EchoPlugin registrado en capacitorDidLoad") never shows up. Instead, register the plugin in AppDelegate.swift using: import Capacitor func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil ) -> Bool { bridge = CAPBridge(self.window!) CAPBridge.registerPlugin(EchoPlugin.self) return tru
Does above worked for you?
Thanks for the help, but for now I still can’t get it to work. Add this errors on this post
You should not be touching CAPBridge directly anymore. Instead, Capacitor automatically discovers Swift plugins if you set them up correctly. Updated code in answer.
0

I was finally able to get the plugins working on iOS, but I had to do it using the Capacitor plugin generation command, since for now I haven’t found a way to create these plugins individually or independently.
It has to go through the full flow of creating plugins for Ionic, Web, and iOS.
Thanks for your reply.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.