Plugin

public protocol Plugin

Protocol for extending SDK functionality via plugins.

Plugins provide a way to extend the functionality of the LaunchDarkly SDK. A plugin can register hooks that are called during flag evaluation, and can perform initialization when registered with a client instance.

Plugin implementations should be thread-safe as they may be called concurrently from multiple threads.

Usage Example

class MyPlugin: Plugin {
    func getMetadata() -> PluginMetadata {
        return PluginMetadata(name: "MyPlugin")
    }

    func register(client: LDClient, metadata: EnvironmentMetadata) {
        // Perform plugin initialization
    }

    func getHooks(metadata: EnvironmentMetadata) -> [Hook] {
        return [MyHook()]
    }
}

let config = LDConfig.Builder(mobileKey: "your-mobile-key")
    .plugins([MyPlugin()])
    .build()
  • Get metadata about the plugin implementation.

    Declaration

    Swift

    func getMetadata() -> PluginMetadata

    Return Value

    The plugin metadata containing identifying information about the plugin.

  • Register the plugin with a client instance.

    This method is called once for each client instance when the SDK is initialized. If the SDK is configured with multiple environments, this method will be called once for each environment with the respective credential. Use the metadata to distinguish environments.

    Declaration

    Swift

    func register(client: LDClient, metadata: EnvironmentMetadata)

    Parameters

    client

    The client instance for the plugin to use

    metadata

    Metadata about the environment where the plugin is running

  • getHooks(metadata:) Default implementation

    Get hooks that should be registered with the SDK.

    This method is called during SDK initialization to collect hooks from all plugins. If the SDK is configured with multiple environments, this method will be called once for each environment. Use the metadata to distinguish environments.

    Default Implementation

    Get hooks that should be registered with the SDK.

    Default implementation returns an empty array. Override this method to provide hooks.

    Declaration

    Swift

    func getHooks(metadata: EnvironmentMetadata) -> [Hook]

    Parameters

    metadata

    Metadata about the environment where the plugin is running

    Return Value

    An array of hooks to be registered with the SDK