英文:
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("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;
}
}
}
答案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(() => {
loader.LoadRequest(Request.GetDefaultRequest());
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论