Admob native ads: 'Ad failed to load : 0'

mahclarkmahclark ✭✭Member ✭✭

This is the same question I asked on stack overflow.

This issue only occurs with my implementation of native ads. Banner ads work fine. I adapted the custom renderers from this blog to work with native ads. I used test ad unit IDs.

Here is my custom renderer for Android:

[assembly: ExportRenderer(typeof(NativeAdView), typeof(NativeRendererAndroid))]
namespace XXX.Droid[https://xamarinhelp.com/admob-xamarin-forms-display-google-ads-mobile-app/](https://xamarinhelp.com/admob-xamarin-forms-display-google-ads-mobile-app/ "https://xamarinhelp.com/admob-xamarin-forms-display-google-ads-mobile-app/")
{
    public class NativeRendererAndroid : ViewRenderer<NativeAdView, AdView>
    {
        public NativeRendererAndroid(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<NativeAdView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null && Control == null)
                SetNativeControl(CreateAdView());
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(AdView.AdUnitId))
                Control.AdUnitId = Element.AdUnitId;
        }

        private AdView CreateAdView()
        {
            var adView = new AdView(Context)
            {
                AdSize = new AdSize(AdSize.FullWidth, AdSize.AutoHeight),
                AdUnitId = Element.AdUnitId
            };

            adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);

            adView.LoadAd(new AdRequest.Builder().Build());

            return adView;
        }
    }
}

And for iOS:

[assembly: ExportRenderer(typeof(NativeAdView), typeof(NativeRendereriOS))]
namespace XXX.iOS
{
    public class NativeRendereriOS : ViewRenderer<NativeAdView, NativeExpressAdView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NativeAdView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                SetNativeControl(CreateNativeAd());
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(NativeExpressAdView.AdUnitID))
                Control.AdUnitID = Element.AdUnitId;
        }

        private NativeExpressAdView CreateNativeAd()
        {
            AdSize adSize = new AdSize();
            adSize.Size = new CGSize(UIScreen.MainScreen.Bounds.Size.Width, 49);

            var nativeAd = new NativeExpressAdView(adSize)
            {
                AdUnitID = Element.AdUnitId,
                RootViewController = GetVisibleViewController()
            };

            nativeAd.LoadRequest(GetRequest());

            Request GetRequest()
            {
                var request = Request.GetDefaultRequest();
                return request;
            }

            return nativeAd;
        }

        private UIViewController GetVisibleViewController()
        {
            var windows = UIApplication.SharedApplication.Windows;
            foreach (var window in windows)
            {
                if (window.RootViewController != null)
                {
                    return window.RootViewController;
                }
            }
            return null;
        }
    }
}

These custom renderers are very similar to the ones linked in the blog above. Perhaps there is something I need to include for native ads that is not used for banner ads?

Once I get the ad loading correctly, I would like to put it in a ListView, but for now I just have it as a View.

Edit: For clarification, when using the above custom renderers, everything builds successfully, but when the ad tries to load, 'Ad failed to load : 0' gets printed in the debug and the NativeAdView remains transparent. This is both both platforms.

Tagged:
Sign In or Register to comment.