Xamarin iOS AdMob NativeAd AdLoader Delegate 从未被调用。

huangapple go评论72阅读模式
英文:

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;
		}
	}
}
英文:

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(&quot;Loading: &quot; + 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(&quot;DidReceiveUnifiedNativeAd&quot;);
	}
	public void DidFailToReceiveAd(AdLoader adLoader, RequestError error)
	{
		Debug.WriteLine(&quot;DidFailToReceiveAd&quot;);
		//base.DidFailToReceiveAd(adLoader, error);
	}

	public void DidFinishLoading(AdLoader adLoader)
	{
		Debug.WriteLine(&quot;DidFinishLoading&quot;);
		//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&lt;AdMobUnifiedNativeAd,UnifiedNativeAdView&gt;
	{
		protected override void OnElementChanged(ElementChangedEventArgs&lt;AdMobUnifiedNativeAd&gt; 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(&quot;Loading: &quot; + loader.IsLoading);
		}

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

答案1

得分: 0

LoadRequest需要在主线程中调用!这解决了问题。

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

I encountered identical problem. Solution was kinda simple.

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

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

huangapple
  • 本文由 发表于 2020年1月6日 23:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定