iosxamarinxamarin.iosadmobunifiednativeadview

Xamarin iOS AdMob NativeAd AdLoader Delegate Never Called


I am trying to implement the iOS renderer for a XF view to display native ads. I can't seem to load the native ad. Using:

try
{
    loader.Delegate = new MyAdLoaderDelegate(renderer);
    loader.LoadRequest(request);

}catch(Exception e)
{
    Debug.WriteLine(e.Message);
}

Debug.WriteLine("Loading: " + loader.IsLoading);

The delegate is getting set properly and after the loader.LoadRequest() call the loader.IsLoading==true, but the methods on MyAdLoaderDelegate are never called. No exception is thrown by the call to LoadRequest() either.

Here is my delegate class:

private class MyAdLoaderDelegate : NSObject, IUnifiedNativeAdLoaderDelegate
{
    private readonly AdMobUnifiedNativeAdRenderer _renderer;

    public MyAdLoaderDelegate(AdMobUnifiedNativeAdRenderer renderer)
    {
        _renderer = renderer;
    }

    public void DidReceiveUnifiedNativeAd(AdLoader adLoader, UnifiedNativeAd nativeAd)
    {
        Debug.WriteLine("DidReceiveUnifiedNativeAd");
    }
    public void DidFailToReceiveAd(AdLoader adLoader, RequestError error)
    {
        Debug.WriteLine("DidFailToReceiveAd");
        //base.DidFailToReceiveAd(adLoader, error);
    }

    public void DidFinishLoading(AdLoader adLoader)
    {
        Debug.WriteLine("DidFinishLoading");
        //base.DidFinishLoading(adLoader);
    }
}

Curiously too the delegate class would not work when inheriting from UnifiedNativeAdLoaderDelegate, the loader delegate was always null. Implementing the interface works though, but no methods are ever hit.

Not sure where to go from here.

Thanks!

EDIT:

[assembly: ExportRenderer(typeof(AdMobUnifiedNativeAd), typeof(AdMobUnifiedNativeAdRenderer))]
namespace XXXX.Mobile.iOS.CustomRenderers.NativeAds
{
    public class AdMobUnifiedNativeAdRenderer : ViewRenderer<AdMobUnifiedNativeAd,UnifiedNativeAdView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<AdMobUnifiedNativeAd> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                CreateAdView(this);
            }
        }

        private void CreateAdView(AdMobUnifiedNativeAdRenderer renderer)
        {

            if (Element == null) return;

            var loader = new AdLoader(
                Element.AdUnitId,
                GetVisibleViewController(),
                new AdLoaderAdType[] { AdLoaderAdType.UnifiedNative },
                new AdLoaderOptions[] {
                    new NativeAdViewAdOptions {PreferredAdChoicesPosition = AdChoicesPosition.TopRightCorner}
                });

            var request = Request.GetDefaultRequest();
            request.TestDevices = new string[] { Request.SimulatorId };

            UnifiedNativeAdView adView = null;
            try
            {
                adView = MyUnifiedAdView.Create();
                SetNativeControl(adView);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            if (adView == null) return;

            try
            {
                loader.Delegate = new MyAdLoaderDelegate(renderer);
                loader.LoadRequest(request);
            }catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            Debug.WriteLine("Loading: " + loader.IsLoading);
        }

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

Solution

  • I encountered identical problem. Solution was kinda simple.

    LoadRequest needs to be called in main thread! This solved the issue.

    Device.BeginInvokeOnMainThread(() => { 
    loader.LoadRequest(Request.GetDefaultRequest());
    });