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'