Xamarin.Forms中的原生Admob广告.是否有现成的解决方案/示例? [英] Native Admob Ads in Xamarin.Forms. Are there any ready-made solutions / examples?

查看:97
本文介绍了Xamarin.Forms中的原生Admob广告.是否有现成的解决方案/示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Xamarin.Forms中实施原生广告.但是我在Xamarin.Forms上找不到任何示例.也许有人举个例子在Xamarin.Forms上使用Native Ads并可以共享.

我试图做的事的例子:

Android:

  [程序集:Dependency(typeof(NativeAd))]命名空间Ads.Droid.Platform.Renderers.Ad{公共类NativeAd:AdListener,INativeAd{上下文上下文= Android.App.Application.Context;NativeExpressAdView mAdView;公共无效Show(){var videoOptions = new VideoOptions.Builder().SetStartMuted(false).Build();var adOptions = new NativeAdOptions.Builder().SetVideoOptions(videoOptions).Build();AdLoader adLoader =新的AdLoader.Builder(上下文,"ca-app-pub-3940256099942544/2247696110").WithNativeAdOptions(adOptions).Build();var request = new AdRequest.Builder();foreach(TestDevice.GetTestDevices()中的可变项)request.AddTestDevice(item);adLoader.LoadAd(request.Build());}}} 

或:

  [程序集:Dependency(typeof(NativeAd))]命名空间Ads.Droid.Platform.Renderers.Ad{公共类NativeAd:AdListener,INativeAd{NativeExpressAdView mAdView;公共无效Show(){mAdView =新的NativeExpressAdView(Android.App.Application.Context){AdUnitId ="ca-app-pub-3940256099942544/2247247110",AdSize = AdSize.MediumRectangle};var request = new AdRequest.Builder();foreach(TestDevice.GetTestDevices()中的可变项)request.AddTestDevice(item);mAdView.LoadAd(request.Build());}}} 

在iOS中没有这样做,但我还需要一个示例.也许有些事情需要改变或完成.请帮助我.

解决方案

您还可以在iOS中使用 GoogleMobileAds .重要的是来自NuGet的 Xamarin.Firebase.iOS.AdMob 包.

在AppDelegate.cs

  ...使用Google.MobileAds;...公共重写布尔FinishedLaunching(UIApplication应用,NSDictionary选项){...MobileAds.Configure("ca-app-pub-3940256099942544/2247247110");...} 

在iOS依赖项中

  [程序集:Xamarin.Forms.Dependency(typeof(NativeiOSAd))]命名空间XXX.iOS{公共类NativeiOSAd:NativeAd{NativeExpressAdView mAdView;公共NativeiOSAd(){}公共无效Show(){AdSize adSize = new AdSize();adSize.Size = new CGSize(UIScreen.MainScreen.Bounds.Size.Width,100);mAdView =新的NativeExpressAdView(adSize){AdUnitID ="ca-app-pub-3940256099942544/2/2247696110"};请求请求= Request.GetDefaultRequest();mAdView.LoadRequest(request);}}} 

请不要忘记将 NSAllowsArbitraryLoads NSAllowsArbitraryLoadsForMedia NSAllowsArbitraryLoadsInWebContent 例外添加到应用程序的Info.plist文件中以禁用ATS限制./p>

I need to implement Native Ads in Xamarin.Forms. But I did not find any examples on Xamarin.Forms. Maybe someone has an example of using Native Ads on Xamarin.Forms and can share.

My example of what I was trying to do:

Android:

[assembly: Dependency(typeof(NativeAd))]
namespace Ads.Droid.Platform.Renderers.Ad
{
    public class NativeAd : AdListener, INativeAd
    {
        Context context = Android.App.Application.Context;
        NativeExpressAdView mAdView;

       public void Show()
        {   
            var videoOptions = new VideoOptions.Builder().SetStartMuted(false).Build();
            var adOptions = new NativeAdOptions.Builder().SetVideoOptions(videoOptions).Build();
            AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110").WithNativeAdOptions(adOptions).Build();

            var request = new AdRequest.Builder();
            foreach (var item in TestDevice.GetTestDevices())
                request.AddTestDevice(item);

            adLoader.LoadAd(request.Build());   
        }
    }
}

or:

[assembly: Dependency(typeof(NativeAd))]
namespace Ads.Droid.Platform.Renderers.Ad
{
    public class NativeAd : AdListener, INativeAd
    {
        NativeExpressAdView mAdView;

       public void Show()
        {   
            mAdView = new NativeExpressAdView(Android.App.Application.Context)
            {
                AdUnitId = "ca-app-pub-3940256099942544/2247696110",
                AdSize = AdSize.MediumRectangle
            };
            var request = new AdRequest.Builder();
            foreach (var item in TestDevice.GetTestDevices())
                request.AddTestDevice(item);
            mAdView.LoadAd(request.Build());    
        }
    }
}

In iOS didn’t do it, but I also need an example. Maybe something needs to be changed or completed. Help me please.

解决方案

You can also use GoogleMobileAds in iOS. Important the package Xamarin.Firebase.iOS.AdMobfrom NuGet.

in AppDelegate.cs

...
using Google.MobileAds;
...

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
   . . .
   MobileAds.Configure("ca-app-pub-3940256099942544/2247696110 ");
   . . . 
} 

in iOS Dependency

[assembly: Xamarin.Forms.Dependency(typeof(NativeiOSAd))]
namespace XXX.iOS
{
    public class NativeiOSAd:NativeAd
    {

        NativeExpressAdView mAdView;

        public NativeiOSAd()
        {
        }

        public void Show()
        {
            AdSize adSize = new AdSize();
            adSize.Size = new CGSize(UIScreen.MainScreen.Bounds.Size.Width, 100);

            mAdView = new NativeExpressAdView(adSize)
            {
                AdUnitID = "ca-app-pub-3940256099942544/2247696110"
            };

            Request request = Request.GetDefaultRequest();


            mAdView.LoadRequest(request);
        }
    }
}

Don't forget add the NSAllowsArbitraryLoads, NSAllowsArbitraryLoadsForMedia, and NSAllowsArbitraryLoadsInWebContent exceptions to your app's Info.plist file to disable ATS restrictions.

这篇关于Xamarin.Forms中的原生Admob广告.是否有现成的解决方案/示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆