Regulating The Number of Interstitial Ads

TomFTomF Member ✭✭
edited April 21 in Xamarin.Forms

Good Afternoon,

I’m a bit of a beginner with C# at the moment (and code in general) - Right now, I have buttons throughout my app. Currently, I can get an interstitial ad to appear every time you click a button. Now, I would like to try to make the app more user-friendly by having interstitials appear intermittently.

Question

Here is my interstitial ad criteria:
1) Have an interstitial ad show after 3 button clicks inside the app (If that's too difficult - 3 button clicks on a page)
2) If an interstitial ad shows up, no interstitials may show inside the entire app until 5 minutes are up.
What do you think is the best way to go about doing this? - some sort of counter and timer in the code?

Here is my current code, this code simply makes interstitials pop up at every button click. Thank you!

In Xamarin.Forms: Page1.xaml.cs (A simple example page that hosts 3 buttons)

<br />using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PreAppStructure
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1 ()
        {

            InitializeComponent ();

            NavigationPage.SetBackButtonTitle(this, "Back");
        }


        void Handle_Clicked1(object sender, System.EventArgs e)
        {

            DependencyService.Get<IInterstitialAds>().ShowAds();

            var page = new Page2();
            Navigation.PushAsync(page);
        }

        void Handle_Clicked2(object sender, System.EventArgs e)
        {

            DependencyService.Get<IInterstitialAds>().ShowAds();

            var page = new Page3();
            Navigation.PushAsync(page);
        }

        void Handle_Clicked3(object sender, System.EventArgs e)
        {
            DependencyService.Get<IInterstitialAds>().ShowAds();

            var page = new Page4();
            Navigation.PushAsync(page);
        }
    }
}

In Xamarin.Forms: IInterstitialAds.cs

<br />using System;
using System.Collections.Generic;
using System.Text;

namespace PreAppStructure
{
    public interface IInterstitialAds
    {
        void ShowAds();
    }
}

In android: InterstitialAdListener.cs

<br />using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Gms.Ads;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Debug = System.Diagnostics.Debug;

namespace PreAppStructure.Droid
{
    public class InterstitialAdListener: AdListener
    {
        readonly InterstitialAd _ad;

        public InterstitialAdListener(InterstitialAd ad)
        {
            _ad = ad;
        }

        public override void OnAdLoaded()
        {
            base.OnAdLoaded();
            try
            {
                if (_ad.IsLoaded)
                {
                    _ad.Show();
                }
                else
                {
                    Debug.WriteLine("Android Interstitial listener: " + _ad.IsLoaded);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Android Interstitial Try Catch listener: " + ex.Message);
            }
        }
    }
}

In android: InterstitialAds.cs

<br />using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PreAppStructure.Droid;
using Android.App;
using Android.Content;
using Android.Gms.Ads;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using PreAppStructure;

[assembly: Xamarin.Forms.Dependency(typeof(InterstitialAds))]

namespace PreAppStructure.Droid
{
    public class InterstitialAds : IInterstitialAds
    {
        private InterstitialAd ads;
        public void ShowAds()
        {
            try
            {
                var context = Android.App.Application.Context;
                ads = new InterstitialAd(context);

                // BELOW IS A GOOGLE TEST AD
                ads.AdUnitId = "ca-app-pub-3940256099942544/1033173712"; //Replace Video Ads

                var intlistener = new InterstitialAdListener(ads);
                intlistener.OnAdLoaded();
                ads.AdListener = intlistener;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Android Interstitial: " + ex.Message);
            }

        }
    }
}
Sign In or Register to comment.