MoEngageModule

public extension MoEngageModule
  • Asynchronous lifecycle events that may suspend and must be awaited by the SDK.

    Use AsyncEvent for operations that must complete before the SDK proceeds, such as flushing data before logout or checking unregister eligibility. Events are delivered via Item.process(event:sdkInstance:) async, which runs on the MoEngageSDKInstance actor’s executor and is awaited before the SDK continues.

    See more

    Declaration

    Swift

    enum AsyncEvent : Hashable, Sendable
  • Synchronous lifecycle events that modules can respond to.

    These events represent key points in a module’s lifecycle or significant SDK state transitions. Events are delivered via Item.process(event:sdkInstance:), which runs on the MoEngageSDKInstance actor’s executor.

    Core Lifecycle Order

    For a typical app session:

    1. .init — called once when module is registered
    2. .start — called after all modules are initialized
    3. .deinit — called when SDK instance is being removed (rare)
    See more

    Declaration

    Swift

    enum Event : Hashable, @unchecked Sendable
  • Metadata about a module including its name, version, and additional information.

    This type provides structured information about SDK modules for:

    • Version tracking: Which module versions are integrated
    • Debugging: Identifying active modules in logs
    • Analytics: Reporting module usage to the backend
    • Configuration: Storing module-specific metadata

    Payload Generation

    When multiple modules are collected, they can be converted to a network payload format for reporting to the MoEngage backend.

    Example

    let info = MoEngageModule.Info(
        name: "cards",
        version: "2.1.0",
        metadata: ["cardTypes": ["carousel", "stack"]]
    )
    
    See more

    Declaration

    Swift

    struct Info
  • Protocol that all SDK modules must conform to.

    Implementing this protocol allows a module to be registered with the SDK and participate in the module lifecycle. All methods are static and receive an isolated MoEngageSDKInstance, so they execute on the instance’s actor without an extra hop.

    Implementation Guidelines

    State Management

    • Store per-instance state keyed by the SDK instance’s workspace ID
    • Clean up state in the Event.deinit case

    Event Handling

    • Return quickly from process(event:sdkInstance:); spawn an unstructured Task { } for expensive or async work you do not need to await
    • Use process(event:sdkInstance:) async only for operations the SDK must await before proceeding (e.g. flushing data before logout)

    Error Handling

    • Don’t throw errors from these methods
    • Log errors internally and handle gracefully

    Example Implementation

    final class AnalyticsModule: MoEngageModule.Item {
    
        static func getInfo(sdkInstance: isolated MoEngageSDKInstance) -> MoEngageModule.Info? {
            return .init(
                name: "analytics",
                version: "3.0.0",
                metadata: ["features": ["events", "userAttributes"]]
            )
        }
    
        static func process(event: MoEngageModule.Event, sdkInstance: isolated MoEngageSDKInstance) {
            switch event {
            case .init:
                setupObservers(for: sdkInstance)
            case .start:
                Task { await trackAppOpened(for: sdkInstance) }
            case .deinit:
                removeState(for: sdkInstance)
            default:
                break
            }
        }
    
        static func process(event: MoEngageModule.AsyncEvent, sdkInstance: isolated MoEngageSDKInstance) async {
            switch event {
            case .willLogout:
                await flushPendingEvents(for: sdkInstance)
            default:
                break
            }
        }
    
        static func listensToAdditionalNotifications() -> [Notification.Name] { [] }
    }
    
    See more

    Declaration

    Swift

    protocol Item : Sendable, ItemReflection
  • Marker protocol that exposes SDK modules to the Objective-C runtime.

    Item inherits from this protocol so the SDK can use NSClassFromString and related reflection APIs to discover and instantiate modules registered by name — without requiring a direct Swift type reference at the call site.

    Important

    Do not conform to ItemReflection directly. Conform to Item instead.

    Declaration

    Swift

    @objc
    protocol ItemReflection