Displaying AdMob ads on iOS with Xamarin Forms!

All the tutorials on displaying ads on iOS are currently broken, as the package used to display ads has been removed.

There is a new package, however the usage example it gives is for an iOS dependent project. It will not work/compile for use in Xamarin Forms

The approach is the same however, You are rendering a View you declare in your portable project

Install the correct ad package, it should look like this in Nuget Package Manager

xamarin

Here is the view I created in my Xamarin Forms project.

namespace CanadianClassifiedsAlerter
{
    public class AdMobView : View
    {
        public AdMobView() { }
    }
}

In my iOS project, I created a ViewRenderer.

[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobRenderer))]
namespace CanadianClassifiedsAlerter.iOS
{
    public class AdMobRenderer : Xamarin.Forms.Platform.iOS.ViewRenderer
    {
        const string bannerId = "THISISWHEREMY_AD_IDGOES";

        BannerView adView;
        bool viewOnScreen = false;

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);
            // Setup your BannerView, review AdSizeCons class for more Ad sizes. 
            adView = new BannerView(size: AdSizeCons.Banner, origin: new CGPoint(-10, 0))
            {
                AdUnitID = bannerId,
                RootViewController = UIApplication.SharedApplication.Windows[0].RootViewController
            };

            // Wire AdReceived event to know when the Ad is ready to be displayed
            adView.AdReceived += (object sender, EventArgs ea) => {
               
                viewOnScreen = true;
            };

            adView.LoadRequest(Request.GetDefaultRequest());
            base.SetNativeControl(adView);
        }
    }


}

Now, after all is said and done to add it to my page, it’s this simple!

 

 mainLayout.Children.Add(new AdMobView
            {
                WidthRequest = 320,
                HeightRequest = 50
            });

Your end result should look like this

iosAdmobFinalResult

EDIT: Sample project files are here

Unused Canadian Classifieds Portable Project Files (Xamarin Forms).

I decided to use Xamarin.iOS instead for this project, so feel free to do whatever with those files.

 

EDIT: A user followed the advice from this article and got the following error…

“Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[AppDelegate window]: unrecognized selector sent to instance 0x79aa9b10”

He override the Window with the following code and away it worked!

public override UIWindow Window {
get
{
return UIApplication.SharedApplication.KeyWindow;
}
}

11 comments for “Displaying AdMob ads on iOS with Xamarin Forms!

  1. James
    August 2, 2016 at 11:18 pm

    Can you please show the Nuget Package Manager image using a higher quality image. I am not able to see it clearly. Or just give the name of the package you used.
    If you have a working sample, it would greatly appreciated.
    As you said the samples are broken. I have also tried samples for Android and Windows phone also seem broken. The problems seem to be with the Nuget packages and code don’t match the versions of the new Xamarin forms.

    • admin
      September 19, 2016 at 11:28 pm

      I will upload a working sample now.

  2. September 15, 2016 at 7:24 am

    Hi
    Nuget package is not clear in the mentioned image. can you post the name of the package? Thanks in advance.

    • admin
      September 17, 2016 at 2:23 am

      I sent you an email. Will also update post. Thanks!

  3. Priya
    September 19, 2016 at 4:50 am

    Thanks for your mail. That works for me.

  4. Priya
    September 19, 2016 at 4:56 am

    But i got an exception like this “Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[AppDelegate window]: unrecognized selector sent to instance 0x79aa9b10”

    I fixed this by overriding Window in AppDelegate.cs

    public override UIWindow Window {
    get
    {
    return UIApplication.SharedApplication.KeyWindow;
    }
    }

    • admin
      September 19, 2016 at 10:50 pm

      I will throw your bit of code at the end of the post in case anyone else runs into the same problem, thanks for the feedback!

  5. Priya
    September 20, 2016 at 6:09 am

    Can you help me to add AdMob ads on Windows Phone 8.1 with Xamarin Forms!

    • admin
      September 20, 2016 at 9:49 pm

      I unfortunately never had plans to port this application over to Windows Phone, you’re on your own there 🙁

  6. December 7, 2016 at 7:40 am

    Hai,

    Your guide helped me more. Thank you. I would like to view InterstitialAd while my app opens. Can you help me to add that InterstitialAd ?? Thanks a lot.

    • admin
      December 7, 2016 at 8:26 pm

      Hi Keerthana, I’ll take a look tonight and let you know.

Leave a Reply

Your email address will not be published. Required fields are marked *