Native Admob Ads in Xamarin.Forms. Are there any ready-made solutions / examples?

你离开我真会死。 提交于 2021-02-15 07:11:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/52624172/native-admob-ads-in-xamarin-forms-are-there-any-ready-made-solutions-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!