iosswiftappylar

How do I trigger the onInitialized event listener in the Appylar iOS SDK?


I have implemented the Appylar iOS SDK, but I can't seem to get the event listeners triggered. I have called the initialize function and the SDK is initialized correctly, but the event listener onInitialized is not triggered. Also, when I show banners, the event listener onBannerShown is not triggered either.

When I call the initialize function, I expect the event listener onInitialized to be triggered:

// Initialize the SDK
AppylarManager.initialize(
    appKey: "my_app_key",
    adTypes: [AdType.banner],
    testMode: true
)

// This is never called
func onInitialized() {
    print("initialized")
}

Solution

  • You need to set the event listeners so that the SDK knows to trigger them. You can do so with delegates. Adapting the Appylar iOS example app with some of the code you posted:

    import Appylar
    import SwiftUI
    
    @main
    struct AppylarSampleApp: App {
        
        init() {
            // Set event listener before initialization
            AppylarManager.setEventListener(delegate: self)
    
            // Initialize the SDK
            AppylarManager.initialize(
                appKey: "my_app_key", // The unique app key for your app
                adTypes: [AdType.banner], // The ad types that you want to show
                testMode: true // Test mode, true for development, false for production
            )
        }
        
        // Insert your own view here    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    extension AppylarSampleApp: AppylarDelegate {
        // Event listener triggered at successful initialization
        func onInitialized() {
            print("initialized")
        }
    }
    

    Also, when I show banners, the event listener onBannerShown is not triggered either.

    The same example shows how to set bannerDelegate as well. Modify setEventListener:

            // Set event listener before initialization
            AppylarManager.setEventListener(
              delegate: self,
              bannerDelegate: self // <--- add this
           )
    

    And add another extension:

    extension AppylarSampleApp: BannerViewDelegate {
        // Event listener triggered when a banner is shown
        func onBannerShown(_ height: Int) {
            print("banner shown: \(height)")
        }
    }
    

    Historical note: I previously unintentionally answered this in Staging Ground