I am trying to implement banner ads in xamarin forms but nothing shows up after executing this code. Kindly help me how do I show banner ads in xamarin.ios with Admob. Or what's wrong with this code?
AddViewRenderer.cs
[assembly: ExportRenderer(typeof(AdControlView), typeof(AdMobRenderer))]
namespace AdMobTesting.iOS
{
public class AdMobRenderer : ViewRenderer<AdControlView,BannerView>
{
const string AdmobID = "ca-app-pub-7901894761393287/8231209126";
BannerView adView;
bool viewOnScreen;
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
return;
if (e.OldElement == null)
{
adView = new BannerView(AdSizeCons.SmartBannerPortrait)
{
AdUnitID = AdmobID,
RootViewController = UIApplication.SharedApplication.Windows[0].RootViewController
};
adView.AdReceived += (sender, args) =>
{
if (!viewOnScreen) this.AddSubview(adView);
viewOnScreen = true;
};
adView.LoadRequest(Request.GetDefaultRequest());
base.SetNativeControl(adView);
}
}
}
}
AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
MobileAds.Configure("ca-app-pub-7901894761393287~7049155248");
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
AdMobTestingPage.Xaml
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AdMobTesting" x:Class="AdMobTesting.AdMobTestingPage"
xmlns:ui="clr-namespace:AdMobTesting;assembly:AdMobTesting"
>
<AbsoluteLayout>
<ui:AdControlView AbsoluteLayout.LayoutBounds="0,0.9,1,0.1" AbsoluteLayout.LayoutFlags="All" />
</AbsoluteLayout>
</ContentPage>
AdControlView.cs
public class AdControlView : View
{
}