I would like to use Admob on iOS. (banner)
- I have installed Xamarin.Google.iOS.MobileAds.
- I have added “GADApplicationIdentifier” and set “GADIsAdManagerApp” to true in Info.plist.
- 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.