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.
To import the MediafyGoogleMobileAdsAdapter into your iOS project open your project's Podfile and add the following line to your app's target:
MediafyGoogleMobileAdsAdapter
pod 'MeidafySDK' pod 'MediafyGoogleMobileAdsAdapter'
Then, from the command line run:
pod install --repo-update
Follow the integration instructions to add the SDK to your app.
// ... import GoogleMobileAds // ... GADMobileAds.sharedInstance().start()
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>
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.
GADMobileAds.sharedInstance().start()
adView:didFailToReceiveAdWithError:
To avoid the error, add the following line to your app right after initialization of GMA SDK:
// ... import MediafyGoogleMobileAdsAdapter // ... MediafyGADMediationAdapterInitializer.start()
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.
In order to integrate AdMob native ads with Mars adapter, follow these steps:
Import MediafyGoogleMobileAdsAdapter.
import MediafyGoogleMobileAdsAdapter
Create a GADAdLoader with adUnitID.
adUnitID
let adLoader = GADAdLoader(adUnitID: adUnitId, rootViewController: self, adTypes: [.native], options: [])
Configure the GADAdLoader.
adLoader.delegate = self
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.
MediafyNativeParameters
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
Create a MediafyGADExtras and setup it up with MediafyNativeParameters created in the previous step.
MediafyGADExtras
let extras = MediafyGADExtras(nativeParameters: parameters)
Create a GADRequest.
GADRequest
let request = GADRequest()
Register the MediafyGADExtras.
request.register(extras)
Load the ad.
adLoader.load(request)
Create a layout for your ad according to AdMob documentation.
Add native ad to you UI in adLoader(_ adLoader:, didReceive nativeAd:) GADNativeAdLoaderDelegate delegate method.
adLoader(_ adLoader:, didReceive nativeAd:)
GADNativeAdLoaderDelegate
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 }