Intro

With this Plugin you can add a Google Admob Ads inside your Xamarin Android and iOS Projects with a single line!!! This plugin supports: Banners, Interstitial, Rewarded Videos and Rewearded Interstitials (from version 1.9)

Commerical support

If you need faster support with this plugin, custom changes or in general with your Xamarin or MAUI project, contact me at [email protected] to discuss it further.

Important

From version 2.1.0, I've added support for the consent required by Google for all the final users living in Europe. This support for UMP is only available in the licensed version of the plugin. Without the license, the plugin will continue to work as usual with all the other features normally available. This will allow me to continue supporting the development of the plugin. You can buy the license here.
If you are a charity or your app is used to support a good cause, contact me and I'll verify and send you a free license.

If you prefer to not buy a license, if possible, please, support my work with few coffees or even better with a Membership! You can do it here: Buy Me A Coffee Your help allows me to continue to spend time on this project and continue to maintain and update it with new features and to be ready for the new Google SDK: Google SDK Migration.

Cryptography

This plugin uses an cryptographic algorithm to handle the license.

Current Status

Latest version: 2.3.0

Android iOS Windows Mac
Banner
Interstitial
Rewarded
Rewarded Interstitial *
Open Ads
UMP Consent

*They are implemented but currently they are not working. Probably something in the Admob SDK. I'm investigating it.

Methods

Banner Interstitial Rewarded Rewarded Interstitial App Open Ads
LoadAd LoadInterstitial LoadRewarded LoadRewardedInterstitial -
ShowInterstitial ShowRewarded ShowRewardedInterstitial
IsInterstitialLoaded IsRewardedLoaded IsRewardedInterstitialLoaded

Events

Banner Interstitial Rewarded Rewarded Interstitial App Open Ads
AdsLoaded OnInterstitialLoaded OnRewardedLoaded OnRewardedLoaded OnAppOpenAdLoaded
AdsFailedToLoad OnInterstitialFailedToLoad OnRewardedFailedToLoad OnRewardedFailedToLoad OnAppOpenFailedToLoad
AdsImpression OnInterstitialImpression OnRewardedImpression OnRewardedImpression OnAppOpenImpression**
AdsClicked OnInterstitialOpened OnRewardedOpened OnRewardedOpened OnAppOpenOpened**
AdsOpened OnInterstitialFailedToShow OnRewardedFailedToShow OnRewardedFailedToShow OnAppOpenFailedToShow**
AdsClosed OnInterstitialClosed OnRewardedClosed OnRewardedClosed OnAppOpenClosed**
AdsSwiped OnInterstitialClicked* OnRewardedClicked* OnRewardedClicked* OnAppOpenClicked**
OnUserEarnedReward OnUserEarnedReward

*Only supported on iOS

**Currently only working on Android. I plan to add them on iOS in the next version

Initialization

From your platform code, from version 2.1.0, you need to call:


                                    CrossMTAdmob.Current.Init(this,...);
                                
                                

If you don't you'll get an error!

You don't need to call:

MobileAds.Initialize(this); (Android)

or

MobileAds.SharedInstance.Start(CompletionHandler); (iOS)

The plugin will do it for you!

Android

You need to add your Admob APPLICATION_ID to your AppManifest:

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="YOUR APPLICATION ID" />

iOS

You need to add GADApplicationIdentifier to your Info.plist. Edit your info.plist adding these Keys:

  <key>GADApplicationIdentifier</key>
<string>YOUR APPLICATION ID</string>
<key>GADIsAdManagerApp</key>
<true/>

If you don't your app will crash

Important for iOS

In case the plugin doesn't install automatically the nuget package:

Xamarin.Google.iOS.MobileAds

you need to add it manually.

Banners

To add a Banner on a page you have two options:

XAML

<controls:MTAdView x:Name="myAds"></controls:MTAdView>

remember to add this line in your XAML:

xmlns:controls="clr-namespace:MarcTron.Plugin.Controls;assembly=Plugin.MtAdmob"

CODE

MTAdView ads = new MTAdView();

Now you can add the control to your layout.

PLEASE NOTE

From version 1.1.0, for Consent compliancy, to load your banner, you need to manually call:

YourBanner.LoadAd();

IMPORTANT

To test the banner during the development google uses two Banner Id, one for Android and the other for iOS. Use them then remember to replace them with your own IDs:

Android: ca-app-pub-3940256099942544/6300978111
iOS: ca-app-pub-3940256099942544/2934735716

If the Banners don't appear in your app, probably it's a size problem. To solve it, add this style to your app.xaml:

<Style TargetType="controls:MTAdView">
<Setter Property="HeightRequest">
<Setter.Value>
<x:OnIdiom Phone="50" Tablet="90"></x:OnIdiom>
</Setter.Value>
</Setter>
</Style>

PROPERTIES

For each AdView if you want, you can set this property: AdsId: To add the id of your ads AdSize: To chose the size of your banners (from version 1.9) AutoSize: If true, the plugin will update the heightbanner height according to the ads loaded (from version 1.9)

For GDPR it's better to rely on a custom consent instead or using the non personalized ads as I cannot guarantee it works. So it's better if you create a custom consent

GLOBAL PROPERTIES

AdsId: To add the id of your ads

PersonalizedAds: You can set it to False if you want to use generic ads (for GDPR...) (It works only for Android Banners, for the others, you must ask for consent) For GDPR it's better to rely on a custom consent instead or using the non personalized ads as I cannot guarantee it works. So it's better if you create a custom consent

TestDevices: You can add here the ID of your test devices

UseRestrictedDataProcessing: For compliance with the CCPA

You can use Global Properties in this way: CrossMTAdmob.Current.UserPersonalizedAds = true;

Interstitials

You can show an interstitial with a single line of code:

CrossMTAdmob.Current.ShowInterstitial();

To Load an interstitial you can use this line:

CrossMTAdmob.Current.LoadInterstitial("xx-xxx-xxx-xxxxxxxxxxxxxxxxx/xxxxxxxxxx");

Rewarded ads

You can show a Rewarded video with a single line of code:

CrossMTAdmob.Current.ShowRewarded();

To Load a Rewarded Video you can use this line:

CrossMTAdmob.Current.LoadRewarded("xx-xxx-xxx-xxxxxxxxxxxxxxxxx/xxxxxxxxxx");

Rewarded Interstitials

You can show a Rewarded video with a single line of code:

CrossMTAdmob.Current.ShowRewardInterstitial();

To Load a Rewarded Video you can use this line:

CrossMTAdmob.Current.LoadRewardInterstitial("xx-xxx-xxx-xxxxxxxxxxxxxxxxx/xxxxxxxxxx");

App Open Ads

To enable the App Open Ads in you apps, you can buy the license here.

The plugin provides functionality to load and display App Open ads in an Android and iOS application, while handling ad loading callbacks and error handling.

To initialize the Open Ads in your app, you need to set the corresponding variable enableOpenAds in

CrossMTAdmob.Current.Init(...)

In the same Init, you also need to pass your Open App Id in openAdsId.

To load and show your Open Ads when the app starts or goes into foreground, you need the following code:

Android

protected override void OnResume()
{
base.OnResume();
CrossMauiMTAdmob.Current.OnResume();
}

iOS

public override void OnActivated(UIApplication application)
{
base.OnActivated(application);
CrossMTAdmob.Current.OnResume();
}

Methods

None

Events

  • OnAppOpenAdLoaded: Event raised when an app open ad is loaded.
  • OnAppOpenOpened: Event raised when an app open ad is opened.
  • OnAppOpenClosed: Event raised when an app open ad is closed.
  • OnAppOpenFailedToLoad: Event raised when an app open ad fails to load.
  • OnAppOpenFailedToShow: Event raised when an app open ad fails to show.
  • OnAppOpenImpression: Event raised when an app open ad impression occurs.
  • OnAppOpenClicked: Event raised when an app open ad is clicked.

Native Ads

To enable the Native Ads in you apps, you can buy the license here.

Native ads are ad assets that are presented to users through UI components that are native to the platform. They're shown using the same types of views with which you're already building your layouts, and can be formatted to match your app's visual design.

Methods

  • LoadNativeAd: To load a native ad. (Eg. CrossMauiMTAdmob.Current.LoadNativeAd();)
  • LoadNativeAds: To load up to 5 native ads (*only on Android) (Eg. CrossMauiMTAdmob.Current.LoadNativeAds(5);)
  • ShowNativeAd: Event raised when a native ad is opened. (Eg. myNativeAds.ShowNativeAd();)

Events

  • OnNativeAdLoaded: Event raised when a native ad is loaded.
  • OnNativeAdOpened: Event raised when a native ad is opened.
  • OnNativeAdClosed: Event raised when a native ad is closed.
  • OnNativeAdFailedToLoad: Event raised when a native ad fails to load.
  • OnNativeAdFailedToShow: Event raised when a native ad fails to show.
  • OnNativeAdImpression: Event raised when a native ad impression occurs.
  • OnNativeAdClicked: Event raised when a native ad is clicked.

Ump

Under the Google EU User Consent Policy, you must make certain disclosures to your users in the European Economic Area (EEA) along with the UK and obtain their consent to use cookies or other local storage, where legally required, and to use personal data (such as AdID) to serve ads. The licensed version of the plugin allows you to automatically ask consent to your users that need to provide it (EEA & UK). To enable this feature and automatically request the consent on your apps, you can buy the license here.

Known issues and fix

iOS: Error: clang++ exited with code 1: Undefined symbols for architecture arm64: "OBJC_CLASS$_UMPConsentForm"...
If you get this error, simply add the package
"Xamarin.Google.iOS.UserMessagingPlatform" Version="1.1.0.1"
to your iOS project.
iOS: The '_GoogleUserMessagingPlatformAssemblyName' start tag on line 3 position 6 does not match the end tag of '_GoogleMobileAdsAssemblyName'. Line 3, position 132. /packages/xamarin.google.ios.usermessagingplatform/1.1.0.1/buildTransitive/Xamarin.Google.iOS.UserMessagingPlatform.targets
In your nuget cache, open the file "xamarin.google.ios.usermessagingplatform\1.1.0.1\buildTransitive\Xamarin.Google.iOS.UserMessagingPlatform.targets"
Replace the line
<_GoogleUserMessagingPlatformAssemblyName> Google.UserMessagingPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
with
<_GoogleUserMessagingPlatformAssemblyName>Google.UserMessagingPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
.Net 7: Error: Detected package downgrade: Xamarin.AndroidX.Lifecycle.LiveData from 2.6.2.3 to 2.5.1.1. Reference the package directly from the project to select a different version.
In your Android project, import the package:
Xamarin.AndroidX.Lifecycle.LiveData Version="2.6.2.3"
Error in ...\xamarin.androidx.lifecycle.common\2.6.2.3\buildTransitive\net7.0-android33.0\..\..\jar\androidx.lifecycle.lifecycle-common.jar:androidx/lifecycle/DispatchQueue.class: Type androidx.lifecycle.DispatchQueue is defined multiple times
In your Android project, import the packages:
"Xamarin.AndroidX.Activity" Version="1.8.2"
"Xamarin.AndroidX.Activity.Ktx" Version="1.8.2"