Mars SDK - Banner Ad Unit

Add Banner

To load and show banner ads, you should initialize, configure, and add the MediafyAdView object to the view hierarchy and call the loadAd() method.

/// ...
private var adView: MediafyAdView!

/// ...

private func loadAd() {

    // 1. Create a MediafyAdView
    adView = MediafyAdView(
        frame: CGRect(origin: .zero, size: CGSize(width: 300, height: 250))
    )
    
    // 2. Configure the MediafyAdView
    adView.adUnitID = MEDIAFY_AD_UNIT_ID
    adView.adSizes = [CGSize(width: 300, height: 250)]

    adView.delegate = self
    
    // Add the ad view to the app UI
    containerAdView.addSubview(adView)
    
    // 3. Load the ad
    adView.loadAd()
}

The frame initialization parameter of the MediafyAdView object defines the physical size of the ad view in the application layout.

Configure Banner

Once the view is created, you should provide ad request configuration properties.

Configuration Property Reqiured Description
adSizes required One or more ad sizes supported for particular ad placement.
delegate recommended The object implemented the MediafyAdViewDelegate protocol. The Mars SDK will trigger the delegate methods in the appropriate phases of the ad's lifecycle.
adUnitID optional An identifier provided by Mars platform for ad placement.
adFormat optional The format of requested ad. It can be .banner or video. The default value is banner.
refreshInterval optional Refresh period of banner in seconds. The default value is 60 seconds.
adPosition optional Context information for the ad request telling the ad slot position on the screen.

Ensure you provide sizes for your ad unit in code or via settings on the publisher's UI. It's a critical property.

Configure Video Ads

If you need to integrate video ads, you can also use the MediafyAdView object in the same way as for banner ads. The single required change is you should explicitly set the ad format via the respective property:        

adView.adFormat = .video

If you set the .video ad format, the Mars SDK will request video placement ads and render the respective (VAST) creatives.

Manage the Ad States

To subscribe to the ad’s lifecycle events, you should implement the MediafyAdViewDelegate protocol:

extension MyViewController: MediafyAdViewDelegate {
    
    func adViewPresentationController() -> UIViewController? {
        self
    }
    
    func adView(_ adView: MediafyAdView, didFailToReceiveAdWith error: any Error) {
        print("Did fail to receive ad with error: \(error.localizedDescription)")
    }
    
    func adView(_ adView: MediafyAdView, didReceiveAdWithAdSize adSize: CGSize) {
        // Called when ad is loaded
    }
    
    func adViewWillPresentModal(_ adView: MediafyAdView) {
        // Called when modal is about to be presented
    }
    
    func adViewDidDismissModal(_ adView: MediafyAdView) {
        // Called when modal is dismissed
    }
    
    func adViewWillLeaveApplication(_ adView: MediafyAdView) {
        // Called when the application is about to enter the background
    }
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us