Admob With IOS

I would like to use Admob on iOS. (banner)

  1. I have installed Xamarin.Google.iOS.MobileAds.
  2. I have added “GADApplicationIdentifier” and set “GADIsAdManagerApp” to true in Info.plist.
  3. In Program.cs, I have tried the following:
using System;
using Foundation;
using Google.MobileAds;
using UIKit;

namespace TestAdmob
{
    [Register("AppDelegate")]
    class Program : UIApplicationDelegate
    {
        private static Game1 game;
        private BannerView bannerView;

        internal static void RunGame()
        {
            game = new Game1();
            game.Run();
        }

        static void Main(string[] args)
        {
            UIApplication.Main(args, null, typeof(Program).Name);
        }

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            MobileAds.SharedInstance.Start(CompletionHandler);
            return base.FinishedLaunching(application, launchOptions);
        }

        private void CompletionHandler(InitializationStatus status) { }

        public override void WillTerminate(UIApplication application)
        {
            bannerView?.Dispose();
            base.WillTerminate(application);
        }

        public override void FinishedLaunching(UIApplication app)
        {
            UIWindow window = null;

            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                window = UIApplication.SharedApplication.KeyWindow;
            }
            else
            {
                window = UIApplication.SharedApplication.Windows[0];
            }

            if (window != null && window.RootViewController != null)
            {
                bannerView = new BannerView(AdSizeCons.Banner)
                {
                    AdUnitId = "My-ID",
                    RootViewController = window.RootViewController
                };
                bannerView.LoadRequest(Request.GetDefaultRequest());
                bannerView.TranslatesAutoresizingMaskIntoConstraints = false;

                window.RootViewController.View.AddSubview(bannerView);

                NSLayoutConstraint.ActivateConstraints(new[]
                {
                    bannerView.BottomAnchor.ConstraintEqualTo(window.RootViewController.View.SafeAreaLayoutGuide.BottomAnchor),
                    bannerView.CenterXAnchor.ConstraintEqualTo(window.RootViewController.View.CenterXAnchor),
                });

                RunGame();
            }
        }
    }
}

But it is not working. Do you have any ideas?
Thank you in advance.

Hi, While tinkering, I managed to compile without errors, but the ad still doesn’t show up. Here’s the code:

using UIKit;
using Google.MobileAds;
using Foundation;

namespace MyAdmobProject
{
    [Register("AppDelegate")]
    class Program : UIApplicationDelegate
    {
        private static Game1 game;
        private UIWindow window;
        private BannerView bannerView;

        internal static void RunGame()
        {
            game = new Game1();
            game.Run();
        }

        static void Main(string[] args)
        {
            UIApplication.Main(args, null, typeof(Program));
        }

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            window = new UIWindow(UIScreen.MainScreen.Bounds);
            window.RootViewController = new UIViewController();
            window.MakeKeyAndVisible();

            LoadAdMobBanner();

            RunGame();

            return true;
        }
       
        private void LoadAdMobBanner()
        {
            // Replace with your AdMob ad unit ID
            string adUnitId = "ca-app-pub-..........."; //My ID

            bannerView = new BannerView(AdSizeCons.Banner)
            {
                AdUnitId = adUnitId,
                RootViewController = window.RootViewController
            };

            bannerView.LoadRequest(Request.GetDefaultRequest());

            window.RootViewController.View.AddSubview(bannerView);

            bannerView.TranslatesAutoresizingMaskIntoConstraints = false;

            NSLayoutConstraint.ActivateConstraints(new[]
            {
                bannerView.BottomAnchor.ConstraintEqualTo(window.RootViewController.View.SafeAreaLayoutGuide.BottomAnchor),
                bannerView.CenterXAnchor.ConstraintEqualTo(window.RootViewController.View.CenterXAnchor)
            });
        }
    }
}

Has anyone encountered this kind of problem before?

Hi, I managed to display the banner above the game window in the Program.cs class. However, the touch event no longer passes to the game window. Does anyone know how to send events to the underlying window? Thank you in advance :grinning:.