To load and show banner ads, you should initialize, configure, and add the MediafyAdView object to the view hierarchy and call the loadAd() method.
MediafyAdView
loadAd()
/// ... 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.
frame
Once the view is created, you should provide ad request configuration properties.
adSizes
delegate
MediafyAdViewDelegate
adUnitID
adFormat
.banner
video
banner
refreshInterval
adPosition
Ensure you provide sizes for your ad unit in code or via settings on the publisher's UI. It's a critical property.
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.
.video
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 } }