【问题标题】:Is it possible to integrate AdMob in an Android app using Uno Platform是否可以使用 Uno 平台将 AdMob 集成到 Android 应用程序中
【发布时间】:2020-01-22 12:30:42
【问题描述】:

我有一些 UWP 应用想迁移到 Android。

我已经使用 Xamarin.Forms 迁移了一些 我发现了似乎很棒的 Uno Platform。但是我没有找到任何关于在使用 Uno Platform 的 Android 项目中集成 AdMob 广告的信息。

有人做过吗?

【问题讨论】:

    标签: xamarin.forms admob uno-platform


    【解决方案1】:

    是的,这是可能的,我已经能够让它在我的 Android 和 iOS 上的 Uno Platform 应用程序中运行。我计划写一封blogpost 来讨论让 AdMob 和 AdSense 在 Android、iOS 和 WASM 上运行,并在 NuGet 上发布一个 Uno 平台库,它将为您完成所有繁重的工作,敬请期待 :-)。

    目前,这里是我当前使用的控件的未经编辑的原始版本。它要求您在 Android projectiOS project 中安装 Google Play Services Ads NuGet 包。

    安卓

    #if __ANDROID__
    using Android.Gms.Ads;
    using Android.Widget;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Uno.UI;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace SmsTicket.Core.Controls
    {
        public partial class AdControl : ContentControl
        {
            public AdControl()
            {
                var adView = new AdView(ContextHelper.Current);
                adView.AdSize = AdSize.SmartBanner;
                adView.AdUnitId = "YOUR AD UNIT ID";
                HorizontalContentAlignment = HorizontalAlignment.Stretch;
                VerticalContentAlignment = VerticalAlignment.Stretch;
                var adParams = new LinearLayout.LayoutParams(
                    LayoutParams.WrapContent, LayoutParams.WrapContent);
                adView.LayoutParameters = adParams;           
                adView.LoadAd(new AdRequest.Builder().AddTestDevice("YOUR TEST DEVICE ID").Build());
                Content = adView;
            }
        }
    }
    #endif
    

    iOS

    #if __IOS__
    using Google.MobileAds;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using UIKit;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml;
    using CoreGraphics;
    
    namespace SmsTicket.Core.Controls
    {
        public partial class AdControl : ContentControl
        {
            public AdControl()
            {
                HorizontalContentAlignment = HorizontalAlignment.Stretch;
                VerticalContentAlignment = VerticalAlignment.Stretch;
                Background = SolidColorBrushHelper.Red;
                Width = AdSizeCons.LargeBanner.Size.Width;
                Height = AdSizeCons.LargeBanner.Size.Height;
                Windows.UI.Xaml.Window.Current.Activated += Current_Activated;
            }
    
            private void LoadAd()
            {
                if (!(Content is BannerView))
                {
                    var adView = new BannerView(AdSizeCons.LargeBanner)
                    {
                        AdUnitID = "YOUR AD UNIT ID",
                        RootViewController = GetVisibleViewController()
                    };
                    adView.LoadRequest(GetRequest());
                    Content = adView;
                }
            }
    
            Request GetRequest()
            {
                var request = Request.GetDefaultRequest();
                // Requests test ads on devices you specify. Your test device ID is printed to the console when
                // an ad request is made. GADBannerView automatically returns test ads when running on a
                // simulator. After you get your device ID, add it here
                request.TestDevices = new[] { Request.SimulatorId.ToString(), "YOUR TEST DEVICE ID" };
                return request;
            }
    
            UIViewController GetVisibleViewController()
            {
                UIViewController rootController;
                if (UIApplication.SharedApplication.KeyWindow == null)
                {
                    return null;
                }
                else
                {
                    rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                }
    
                if (rootController.PresentedViewController == null)
                    return rootController;
    
                if (rootController.PresentedViewController is UINavigationController)
                {
                    return ((UINavigationController)rootController.PresentedViewController).VisibleViewController;
                }
    
                if (rootController.PresentedViewController is UITabBarController)
                {
                    return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
                }
    
                return rootController.PresentedViewController;
            }
    
            private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
            {
                LoadAd();
            }
        }
    }
    #endif
    

    还要确保仅有条件地包含广告控件(因为我在这里只提供了 Android 和 iOS 版本)。

    【讨论】:

    • 很高兴它有帮助 :-) !
    • @MartinZikmund 谢谢!你有没有开始写关于广告的博文??
    • @Maximus 啊,还没有。不过,我会把它放在我的积压工作之上!
    • 太棒了!谢谢?
    【解决方案2】:

    Uno Platform 不会阻止您使用任何第三方(尤其是在移动设备上)。如果存在 xamarin 绑定,则可以在代码中按原样使用它,就像在 Xamarin.Forms 应用程序中使用它一样。如果它是一个影响视图的库,您很可能会创建一个自定义控件并通过 C# 与第三方类进行交互。

    如果库没有 xamarin 绑定,您可以创建以下 Microsoft 文档。

    好消息!对于 admob,有一个 microsoft 支持的绑定 nuget,它过去曾在 uno 应用程序中使用过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2011-05-07
      • 1970-01-01
      • 2015-02-03
      • 2013-04-18
      • 1970-01-01
      相关资源
      最近更新 更多