Xamarin Google本地广告iOS

发布于 2025-02-13 05:17:56 字数 6796 浏览 1 评论 0原文

我正在尝试在Xamarin中实现本地广告,它可以与Android一起使用,但我对iOS有问题 这是我的代码,

public class AdMobNativeAdRenderer : ViewRenderer<Controls.NativeAdView, UnifiedNativeAdView>, IAdLoaderDelegate, IUnifiedNativeAdDelegate, IUnifiedNativeAdLoaderDelegate, IVideoControllerDelegate

    {

        
        public UnifiedNativeAdView nativeAdView = null;
        public void DidReceiveUnifiedNativeAd(AdLoader adLoader, UnifiedNativeAd nativeAd)
        {

            // A unified native ad has loaded, and can be displayed.
            nativeAd.Delegate = this;
            //  The headline and mediaContent are guaranteed to be present in every native ad.
            nativeAdView.NativeAd = nativeAd;
            if (nativeAdView.HeadlineView==null)
            {
                nativeAdView.HeadlineView= new UILabel();
            }
            (nativeAdView.HeadlineView as UILabel).Text = nativeAd.Headline;
            if (nativeAdView.MediaView == null) {
                nativeAdView.MediaView = new MediaView();
             }
            if (nativeAdView.MediaView.MediaContent == null)
            {
                nativeAdView.MediaView.MediaContent = new MediaContent();
            }
            nativeAdView.MediaView.MediaContent = nativeAd.MediaContent;
            nativeAdView.MediaView.ContentMode = UIViewContentMode.ScaleAspectFill;
            if (nativeAdView.BodyView==null)
            {
                nativeAdView.BodyView = new UILabel();
            }
            (nativeAdView.BodyView as UILabel).Text = nativeAd.Body;
            nativeAdView.BodyView.Hidden = nativeAd.Body == null;
            if (nativeAdView.CallToActionView==null)
            {
                nativeAdView.CallToActionView = new UIButton();
            }
       
            (nativeAdView.CallToActionView as UIButton).SetTitle(nativeAd.CallToAction,UIControlState.Normal);
            nativeAdView.CallToActionView.Hidden = nativeAd.CallToAction == null;

            if (nativeAdView.IconView==null)
            {
                nativeAdView.IconView = new UIImageView();
            }
            (nativeAdView.IconView as UIImageView).Image = nativeAd.Icon.Image;
            nativeAdView.IconView.Hidden = nativeAd.Icon == null;
            if (nativeAdView.StoreView==null)
            {
                nativeAdView.StoreView = new UILabel();
            }
            (nativeAdView.StoreView as UILabel).Text = nativeAd.Store;
            nativeAdView.StoreView.Hidden = nativeAd.Store == null;

            if (nativeAdView.PriceView==null)
            {
                nativeAdView.PriceView = new UILabel();
            }
            (nativeAdView.PriceView as UILabel).Text = nativeAd.Price;

            nativeAdView.PriceView.Hidden = nativeAd.Price == null;
            if (nativeAdView.AdvertiserView==null)
            {
                nativeAdView.AdvertiserView = new UILabel();
            }
            if (nativeAdView.StoreView==null)
            {
                nativeAdView.StoreView = new UILabel();
            }
            (nativeAdView.StoreView as UILabel).Text = nativeAd.Store;


            (nativeAdView.AdvertiserView as UILabel).Text = nativeAd.Advertiser;
            nativeAdView.AdvertiserView.Hidden = nativeAd.Advertiser == null;
            nativeAdView.CallToActionView.UserInteractionEnabled = false;
            nativeAdView.NativeAd = nativeAd;
            try
            {
               nativeAdView.BackgroundColor = UIColor.Green;
                SetNativeControl(nativeAdView);
                
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }
            
        }

        
        [Export("adLoaderDidFinishLoading:")]
        void DidFinishLoading(AdLoader adLoader)
        {
            // The adLoader has finished loading ads, and a new request can be sent.
        }

        public void DidFailToReceiveAd(AdLoader adLoader, RequestError error)
        {

        }
        protected override void OnElementChanged(ElementChangedEventArgs<Controls.NativeAdView> e)
        {
            base.OnElementChanged(e);
            try
            {
                if (e.OldElement != null)
                {
                    try
                    {
                        e.OldElement.Content=null;
                    }
                    catch (Exception)
                    {


                    }

                    
                    SetNativeControl(nativeAdView);
                }

                if (e.NewElement != null)
                {
                    if (Control == null)
                    {
                        // Instantiate the native control and assign it to the Control property with
                        CreateAdView();
                        try
                        {
                            e.NewElement.Content = null;
                        }
                        catch (Exception)
                        {


                        }

                        SetNativeControl(nativeAdView);

                    }
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }
        AdLoader  adLoader;
        private void CreateAdView()
        {

            //if (Element == null) return;
            var multipleAdsOptions = new MultipleAdsAdLoaderOptions { NumberOfAds = 1 };
            adLoader = new AdLoader("ca-app-pub-3940256099942544/3986624511", GetVisibleViewController(), new[] { AdLoaderType.UnifiedNative }, new[] { multipleAdsOptions })
            {
                Delegate = this
            };

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



            if (nativeAdView == null)
            {
                nativeAdView = new UnifiedNativeAdView();
            };

            try
            {
                adLoader.LoadRequest(request);

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

            Debug.WriteLine("Loading: " + adLoader.IsLoading);
        }
        private UIViewController GetVisibleViewController()
        {
            var windows = UIApplication.SharedApplication.Windows;
            foreach (var window in windows)
            {
                if (window.RootViewController != null)
                {
                    return window.RootViewController;
                }
            }
            return null;
        }

    }

每件事都可以按预期工作,我会收到广告和事件didreceiveunifiednativead火灾,但是我得到了一个空的广告,尽管当我调试代码时,我看到广告的内容不是空的,并且控制颜色是绿色的,所以控件已加载,但是为什么它不显示广告内容 请任何人帮助解决这个问题

I am trying to implement Native ads in xamarin it works fine with android but I have a problem with ios
Here is my code

public class AdMobNativeAdRenderer : ViewRenderer<Controls.NativeAdView, UnifiedNativeAdView>, IAdLoaderDelegate, IUnifiedNativeAdDelegate, IUnifiedNativeAdLoaderDelegate, IVideoControllerDelegate

    {

        
        public UnifiedNativeAdView nativeAdView = null;
        public void DidReceiveUnifiedNativeAd(AdLoader adLoader, UnifiedNativeAd nativeAd)
        {

            // A unified native ad has loaded, and can be displayed.
            nativeAd.Delegate = this;
            //  The headline and mediaContent are guaranteed to be present in every native ad.
            nativeAdView.NativeAd = nativeAd;
            if (nativeAdView.HeadlineView==null)
            {
                nativeAdView.HeadlineView= new UILabel();
            }
            (nativeAdView.HeadlineView as UILabel).Text = nativeAd.Headline;
            if (nativeAdView.MediaView == null) {
                nativeAdView.MediaView = new MediaView();
             }
            if (nativeAdView.MediaView.MediaContent == null)
            {
                nativeAdView.MediaView.MediaContent = new MediaContent();
            }
            nativeAdView.MediaView.MediaContent = nativeAd.MediaContent;
            nativeAdView.MediaView.ContentMode = UIViewContentMode.ScaleAspectFill;
            if (nativeAdView.BodyView==null)
            {
                nativeAdView.BodyView = new UILabel();
            }
            (nativeAdView.BodyView as UILabel).Text = nativeAd.Body;
            nativeAdView.BodyView.Hidden = nativeAd.Body == null;
            if (nativeAdView.CallToActionView==null)
            {
                nativeAdView.CallToActionView = new UIButton();
            }
       
            (nativeAdView.CallToActionView as UIButton).SetTitle(nativeAd.CallToAction,UIControlState.Normal);
            nativeAdView.CallToActionView.Hidden = nativeAd.CallToAction == null;

            if (nativeAdView.IconView==null)
            {
                nativeAdView.IconView = new UIImageView();
            }
            (nativeAdView.IconView as UIImageView).Image = nativeAd.Icon.Image;
            nativeAdView.IconView.Hidden = nativeAd.Icon == null;
            if (nativeAdView.StoreView==null)
            {
                nativeAdView.StoreView = new UILabel();
            }
            (nativeAdView.StoreView as UILabel).Text = nativeAd.Store;
            nativeAdView.StoreView.Hidden = nativeAd.Store == null;

            if (nativeAdView.PriceView==null)
            {
                nativeAdView.PriceView = new UILabel();
            }
            (nativeAdView.PriceView as UILabel).Text = nativeAd.Price;

            nativeAdView.PriceView.Hidden = nativeAd.Price == null;
            if (nativeAdView.AdvertiserView==null)
            {
                nativeAdView.AdvertiserView = new UILabel();
            }
            if (nativeAdView.StoreView==null)
            {
                nativeAdView.StoreView = new UILabel();
            }
            (nativeAdView.StoreView as UILabel).Text = nativeAd.Store;


            (nativeAdView.AdvertiserView as UILabel).Text = nativeAd.Advertiser;
            nativeAdView.AdvertiserView.Hidden = nativeAd.Advertiser == null;
            nativeAdView.CallToActionView.UserInteractionEnabled = false;
            nativeAdView.NativeAd = nativeAd;
            try
            {
               nativeAdView.BackgroundColor = UIColor.Green;
                SetNativeControl(nativeAdView);
                
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }
            
        }

        
        [Export("adLoaderDidFinishLoading:")]
        void DidFinishLoading(AdLoader adLoader)
        {
            // The adLoader has finished loading ads, and a new request can be sent.
        }

        public void DidFailToReceiveAd(AdLoader adLoader, RequestError error)
        {

        }
        protected override void OnElementChanged(ElementChangedEventArgs<Controls.NativeAdView> e)
        {
            base.OnElementChanged(e);
            try
            {
                if (e.OldElement != null)
                {
                    try
                    {
                        e.OldElement.Content=null;
                    }
                    catch (Exception)
                    {


                    }

                    
                    SetNativeControl(nativeAdView);
                }

                if (e.NewElement != null)
                {
                    if (Control == null)
                    {
                        // Instantiate the native control and assign it to the Control property with
                        CreateAdView();
                        try
                        {
                            e.NewElement.Content = null;
                        }
                        catch (Exception)
                        {


                        }

                        SetNativeControl(nativeAdView);

                    }
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }
        AdLoader  adLoader;
        private void CreateAdView()
        {

            //if (Element == null) return;
            var multipleAdsOptions = new MultipleAdsAdLoaderOptions { NumberOfAds = 1 };
            adLoader = new AdLoader("ca-app-pub-3940256099942544/3986624511", GetVisibleViewController(), new[] { AdLoaderType.UnifiedNative }, new[] { multipleAdsOptions })
            {
                Delegate = this
            };

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



            if (nativeAdView == null)
            {
                nativeAdView = new UnifiedNativeAdView();
            };

            try
            {
                adLoader.LoadRequest(request);

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

            Debug.WriteLine("Loading: " + adLoader.IsLoading);
        }
        private UIViewController GetVisibleViewController()
        {
            var windows = UIApplication.SharedApplication.Windows;
            foreach (var window in windows)
            {
                if (window.RootViewController != null)
                {
                    return window.RootViewController;
                }
            }
            return null;
        }

    }

Every thing is working as anticipated and I receive the ad and the event DidReceiveUnifiedNativeAd fires but I get an empty ad although when I debug the code I see that the content of the ad is not empty and the control color is green so the control is loaded but why it doesn't show the ad content
Please can anyone help is solving this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文