Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...