I'm working with a plugin that I would like to manually register in iOS only if some condition is meet. Right now, the autogenerated AppDelegate.swift registers all plugins included in the pubspect.yaml with this line:
GeneratedPluginRegistrant.register(with: self)
Is there any way to avoid registering a single plugin?
Thank you
Yes, there is a way (after browsing and reading Flutter iOS engine documentation).
AppDelegate.swift file in Swift language tooAppDelegate.swift (below the AppDelegate class). This is to make things simple and conciseMy code for the plugin:
// TODO: change to our own plugin name
public class FlutterNativeTimezonePlugin: NSObject, FlutterPlugin {
public static func addManuallyToRegistry(registry: FlutterPluginRegistry) {
// https://api.flutter.dev/objcdoc/Protocols/FlutterPluginRegistry.html#/c:objc(pl)FlutterPluginRegistry(im)registrarForPlugin:
// TODO: change to our own plugin name
let registrar = registry.registrar(forPlugin: "flutter_native_timezone")
if let safeRegistrar = registrar {
register(with: safeRegistrar)
}
}
// this is an override of the following function:
// https://api.flutter.dev/objcdoc/Protocols/FlutterPlugin.html#/c:objc(pl)FlutterPlugin(cm)registerWithRegistrar:
public static func register(with registrar: FlutterPluginRegistrar) {
// TODO: change to our own plugin name
let channel = FlutterMethodChannel(name: "flutter_native_timezone", binaryMessenger: registrar.messenger())
let instance = FlutterNativeTimezonePlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
// TODO: add plugin's method calls
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getLocalTimezone":
result(NSTimeZone.local.identifier)
case "getAvailableTimezones":
result(NSTimeZone.knownTimeZoneNames)
default:
result(FlutterMethodNotImplemented)
}
}
}
Next step:
Below the GeneratedPluginRegistrant.register(with: self) method, add the plugin initialisation: FlutterNativeTimezonePlugin.addManuallyToRegistry(registry: self)
Summary:
This is manual way of adding a plugin to the iOS build. Shame there is no documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With