0

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;
        }
    }
}
jmichas
  • 1,139
  • 1
  • 10
  • 21
  • Can you please show me the other codes in your renderer? [Here](https://stackoverflow.com/questions/52624172/native-admob-ads-in-xamarin-forms-are-there-any-ready-made-solutions-examples) is example about using dependency service to show ads. Also check if you has use the right key. – nevermore Jan 07 '20 at 07:59
  • @JackHua-MSFT added complete renderer code for you. – jmichas Jan 08 '20 at 14:44
  • Did you use override keyword when inheriting from UnifiedNativeAdLoaderDelegate? – nevermore Jan 09 '20 at 08:15
  • @JackHua-MSFT Yes I used override when using UnifiedNativeAdLoaderDelegate. The problem I have is the delegate is never invoked and because of the way the library is organized I have no visibility into what is happening once LoadRequest is called. Is there any compiler switch to allow more verbose debug logging? – jmichas Jan 10 '20 at 10:57
  • Could you provide an example on Github that we can try to test with? – Saamer Jan 10 '20 at 21:32
  • Just put loader as a class level variable and it should work. – Hadi Terkawi Jul 06 '22 at 03:05

1 Answers1

0

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());
});
Seth Rayer
  • 16
  • 2
  • Thanks! I think I actually figured this out a long time ago but forgot to circle back and answer my own question. – jmichas Jul 06 '22 at 13:30