Mars SDK - AdMob Adapter

Integrate Mars SDK and Adapter

The integration of Mars SDK with Google AdMob assumes that the publisher already has an AdMob account and has previously integrated the Google Mobile Ads SDK (GMA SDK) into their application.

See the Google Integration Documentation for the details of the AdMob integration.

Add Dependencies

To import the MediafyGoogleMobileAdsAdapter into your iOS project open your project's Podfile and add the following line to your app's target:

pod 'MeidafySDK'
pod 'MediafyGoogleMobileAdsAdapter'

Then, from the command line run:

pod install --repo-update

Initialize Mars SDK

Follow the integration instructions to add the SDK to your app.

Initialize GoogleMobileAds

// ...
import GoogleMobileAds
// ...
GADMobileAds.sharedInstance().start()

Configure SKAdNetwork

To support Mars network, update your app's Info.plist file to add the keys:

<key>SKAdNetworkItems</key>
<array>
  <dict>
    <key>SKAdNetworkIdentifier</key>
    <string>ejvt5qm6ak.skadnetwork>
  </dict>
</array>

Initialize MediafyAdMobAdapter

The GADMobileAds.sharedInstance().start() should be called in the adapters bundle, otherwise, GMA SDK won’t load the ads with error: adView:didFailToReceiveAdWithError: SDK tried to perform a networking task before being initialized.

To avoid the error, add the following line to your app right after initialization of GMA SDK:

// ...
import MediafyGoogleMobileAdsAdapter
// ...
MediafyGADMediationAdapterInitializer.start()

Integration of banner and interstitial ads

Banner and Interstitial formats don't require any additional effort. The mediation engine of GMA SDK will manage everything according to the Custom Network and Custom events setup. See the AdOps Guide for the details.

Integration of Native ads

In order to integrate AdMob native ads with Mars adapter, follow these steps:

Step 1

Import MediafyGoogleMobileAdsAdapter.

import MediafyGoogleMobileAdsAdapter

Step 2

Create a GADAdLoader with adUnitID.

let adLoader = GADAdLoader(adUnitID: adUnitId, rootViewController: self,
                           adTypes: [.native], options: [])

Step 3

Configure the GADAdLoader.

adLoader.delegate = self

Step 4

Create MediafyNativeParameters with required assets, native event trackers, context and placement types. In the example we request: CTA text, title, icon, image, rating, and description.

let cta = MediafyNativeAssetData(type: .ctatext)
cta.length = 15
    
let title = MediafyNativeAssetTitle(length: 90)
title.required = true
    
let icon = MediafyNativeAssetImage(minimumWidth: 50, minimumHeight: 50)
icon.type = .Icon
    
let image = MediafyNativeAssetImage()
image.required = true
image.width = 1200
image.height = 627
image.type = .Main
    
let description = MediafyNativeAssetData(type: .description)
description.required = true
description.length = 150
    
let rating = MediafyNativeAssetData(type: .rating)
    
let parameters = MediafyNativeParameters()
parameters.assets = [cta, title, icon, image, rating, description]
    
let eventTracker = MediafyNativeEventTracker(
    event: .Impression,
    methods: [.Image, .js]
)
    
parameters.eventtrackers = [eventTracker]
parameters.context = .Social
parameters.placementType = .FeedContent
parameters.contextSubType = .Social

Step 5

Create a MediafyGADExtras and setup it up with MediafyNativeParameters created in the previous step.

let extras = MediafyGADExtras(nativeParameters: parameters)

Step 6

Create a GADRequest.

let request = GADRequest()

Step 7

Register the MediafyGADExtras.

request.register(extras)

Step 8

Load the ad.

adLoader.load(request)

Step 9

Create a layout for your ad according to AdMob documentation.

Step 10

Add native ad to you UI in adLoader(_ adLoader:, didReceive nativeAd:) GADNativeAdLoaderDelegate delegate method.

func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    // Set GADNativeAd in GADNativeAdView
    admobNativeView.nativeAd = nativeAd
    
    // 8. Render the ad
    titleLabel.text = nativeAd.headline
    bodyLabel.text = nativeAd.body
    
    let gadImages = nativeAd.images as? NSArray as? [GADNativeAdImage]
    let image = gadImages?.count == 2 ? gadImages?[1] : gadImages?.first
    
    mainImageView.setImage(
        from: image?.imageURL?.absoluteString,
        placeholder: UIImage(systemName: "photo.artframe")
    )
    
    iconView.setImage(
        from: nativeAd.icon?.imageURL?.absoluteString,
        placeholder: UIImage(systemName: "photo.artframe")
    )
    
    callToActionButton.setTitle(nativeAd.callToAction, for: .normal)
    sponsoredLabel.text = nativeAd.advertiser
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us