Mediation

Delegate Usage

By implementing Mediation delegate protocols you can get notifications about the success, failure, or lifecycle events of the Mediation SDK and its ad objects.

Module Observer Delegate πŸ”—

Implement the ModuleObserver protocol to receive Chartboost Mediation SDK initialization callbacks.

extension ChartboostMediationController: ModuleObserver {
    func onModuleInitializationCompleted(_ result: ModuleInitializationResult) {
        // Chartboost Mediation SDK initialization result is represented by the initialization result of
        // an internal module with module ID `ChartboostMediation.coreModuleID`.
        let moduleName = (result.moduleID == ChartboostMediation.coreModuleID ?
                          "Chartboost Mediation" : "Chartboost Core module \(result.moduleID)")

        if let error = result.error {
            print("[Error] \(moduleName) initialization failed: \(error.localizedDescription)")
            initializationResult = .failure(error)
            completionHandler?(.failure(error))
        } else {
            print("[Success] \(moduleName) initialization succeeded")
            initializationResult = .success(true)
            completionHandler?(.success(true))
        }
    }
}

Interstitial Ad Delegate πŸ”—

Implement the FullscreenAdDelegate protocol to receive notifications about interstitial ads loading, displaying, and closing.

extension FullscreenAdController: FullscreenAdDelegate {
    func didRecordImpression(ad: FullscreenAd) {
        log(action: "record impression", placementName: placementName, error: nil)
    }

    func didClick(ad: FullscreenAd) {
        log(action: "click", placementName: placementName, error: nil)
    }

    func didReward(ad: FullscreenAd) {
        log(action: "get reward", placementName: placementName, error: nil)
    }

    func didClose(ad: FullscreenAd, error: ChartboostMediationError?) {
        log(action: "close", placementName: placementName, error: error)
    }

    func didExpire(ad: FullscreenAd) {
        log(action: "expire", placementName: placementName, error: nil)
    }
}

Implement the BannerAdViewDelegate protocol to receive notifications about banner ads loading, displaying, and closing.

extension BannerAdController: BannerAdViewDelegate {
    func willAppear(bannerView: BannerAdView) {
        // Called when a new ad is about to appear inside of `bannerView`. This method can be used
        // to manually size `bannerView` if desired:
        // if let size = bannerView.size?.size {
        //     bannerView.frame.size = size
        // }
        // This method can also be used to check other updated properties of `bannerView`.
    }
}

Ad Queueing Delegate πŸ”—

Implement the FullscreenAdQueueDelegate protocol to receive notifications on queued events.

extension QueuedAdViewController: FullscreenAdQueueDelegate {
    func fullscreenAdQueue(_ adQueue: FullscreenAdQueue, didFinishLoadingWithResult result: AdLoadResult, numberOfAdsReady: Int) {
        updateUI()
    }

    func fullscreenAdQueueDidRemoveExpiredAd(_ adQueue: FullscreenAdQueue, numberOfAdsReady: Int) {
        print("Expired ad removed from queue, \(numberOfAdsReady) loaded ads remaining.")
    }
}