Using in App purchase to implement `Block Ads’ Feature in Windows Phone app

In my last blog post I explained how to add in app purchase in Windows Phone app. In this post I will show how IAP can be used to implement `Block Ads’ feature. Suppose you have a free app in the marketplace with in app advertisements. Most likely you wish to keep the app free so that it gets the most number of downloads. Still the advertisements may be annoying to some users and it would be great if you let your users remove those ads by making an in app purchase. Similar feature can also be achieved by implementing trial functionality in your app. I believe IAP is easier to implement and provides much more flexibility than trial apps. You have already seen how to set up IAP in your app so I will don’t discuss it again. Here is how you can implement the `Block Ads’ functionality

Step 1. Follow the following tutorial to set up in app purchase in your app

http://www.windowsapptutorials.com/windows-phone/add-in-app-purchases-in-your-windows-phone-app/

Step 2. Set up in app advertising in your app

If you haven’t added in app ads in your app then you could follow this tutorial to add Google Admob in your app.

http://www.windowsapptutorials.com/windows-phone/how-to-use-google-admob-in-windows-phone-app/

Step 3. Create a public Class AppSettings global to the project

Define a public class AppSettings which is global to the whole project with two public data variables DISPLAYADS and ISADBLOCKERACTIVE. You can define a public class by placing it just below the namespace of the project. In the sample provided I have defined it in App.xaml.cs just below the namespace of my project WAT_Test.

Step 4. Code to assign values to DISPLAYADS and ISADBLOCKERACTIVE

We will update the values of these variables when the app is launched and check whether the user has purchased the IAP to remove ads. If he has purchased it then ISADBLOCKERACTIVE will be set to true and DISPLAYADS will be set to false.

in_app

When I discussed how to use google admob in an app, I showed that it can be done through XAML or using C# code. So here too I will discuss how to block ads when it has been placed using XAML or via C#.

Blocking ads that were placed using XAML

  • Add a reference to your project in App.xaml as shown below in file reference.xaml
  • Add a new application resource ApplicationSettings which will use the AppSettings class in the section of Application.Resources as shown below in file local.xaml

in_app

  • Add a value converter class in App.xaml.cs named BooleanToVisibilityConvertor. This class implements IValueConverter converts a boolean value to an equivalent visibility and vice-versa.
  • Add a new application resource BooleanToVisibilityConverter in Application.Resources as shown in local.xaml file below
  • Now you can modify your AdView code for Admob to place a visibility condition as shown in the code adview.xaml

 


Blocking ads that were placed using C#

This is much simpler and you just need to check whether DISPLAYADS is true or false before calling admob() function.

These screen shots show that earlier ads were being displayed in the app but after IAP they didn’t appear. Be assured it wasn’t coincidence as I have getting 100% fill rate for Google Admob for the past few days.

Download full project WAT_Test.zip

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

  • kumar

    your tutorial is nice, but i had a small doubt regarding windows 8 app in-app purchase(not window phone), i had implemented an in-app purchase for my windows 8 app written in html/js, i had used a button to remove ads in my app settings pane, when the user clicks remove ads button only then my function is fired & ads are removed, do i need to include a code to always check the license info of the user during app initialization or once the ads are removed after button is clicked, is it(license & ad-removal) permenantly planted on users pc ???

    • Vivek Maskara

      Yes, you will have to check whether your in app product has been purchased or not every time the app is opened. Have a look at our in app purchase tutorial to get an overview.

      • kumar

        thanx vivek, i even had another doubt regarding windows in-app purchase simulated calls, when i see the window prompt with two buttons “continue” & “cancel” for in-app purchase simulation if i click “continue” button everything goes fine & purchase is succeded, but even if i click “cancel” button the ads are disabled(my ads disabling code is getting executed), do i need to manage special code for the “cancel” button click event or store this automatically when i submit it to the store ??

        • Vivek Maskara

          I am not sure how you have implemented it in windows 8 app but this shouldn’t happen. Try debugging my outputing product purchase status In app_activated event handler. you dont need any sprcial code for cancel button.

          • kumar

            this is the code is used to implement in-app purchase in html/js version of my windows 8 app

            //function to complete purchase process

            function getfull() {

            var currentapp = Windows.ApplicationModel.Store.CurrentAppSimulator;

            var licenseinfo = currentapp.licenseInformation;

            if (!licenseinfo.productLicenses.lookup(“fullversion”).isActive) {

            currentapp.requestAppPurchaseAsync(“fullversion”, true).done(

            function (reciept) {

            //code to delete ads

            $(“#adunit1”).remove();

            $(“#adunit2”).remove();

            $(“#adunit3”).remove();

            $(“#adunit4”).remove();

            $(“#root”).remove();

            }, errorhandler);

            function errorhandler() {

            var msg = Windows.UI.Popups.MessageDialog(“Oops!! Something went wrong, please try again”);

            msg.showAsync();

            }

            }

            else {

            //code to show that user has already bought the feauture of fullversion

            $(“#adunit1”).remove();

            $(“#adunit2”).remove();

            $(“#adunit3”).remove();

            $(“#adunit4”).remove();

            var confirm = Windows.UI.Popups.MessageDialog(“U already bought the Ad-free version”);

            confirm.showAsync();

            }

            }

          • Vivek Maskara

            The problem seems eith your else code. comment the remove ads code from else part and try running your app

          • kumar

            i commented the remove ads code & tried but still the result is same
            🙁

          • Sorry, then i am not very sure about the error. Try asking a question on stackoverflow.com

          • kumar

            after a little bit of trial & error on my code , i came up with this solution & its working perfectly fine now 🙂 , here is the code i want to share

            // the code which manages the “cancel” button click event

            currentapp.requestAppPurchaseAsync(“fullversion”, false).done(
            function () {
            var mym = Windows.UI.Popups.MessageDialog(“You canceled the purchase”);
            mym.showAsync();
            });

          • kumar

            hii vivek, are u sure that i don’t need any special code for “cancel” button that appears in dialog windows of in-app purchase ??

          • As far as as Windows Phone app is concerned, I am sure. Moreover logic suggests that no code is required for cancel event even in Windows 8. There might be some other implementation issues.

  • pythonflash

    Vivek, this was very helpful.
    Question on your code at the top:

    AppSettings.ISADBLOCKERACTIVE = Windows.ApplicationModel.Store.CurrentApp.LicenseInformation.ProductLicenses[“AdBlocker”].IsActive;
    AppSettings.DISPLAYADS = !AppSettings.ISADBLOCKERACTIVE;

    So the ProductLicenses array is built into the library, but you are the one who added the “AdBlocker” element in your own code, somewhere else ?

    • “AdBlocker” is my product identifer which i used in the Windows Phone Dev Portal while adding an in-app product. Replace it with yours.

      • pythonflash

        I see, got it. thanks.

  • sivacharan

    hi i am siva charan,
    i am porting my cocos2d-x code into windows phone app, may know how can i implement in app purchases and trail mode to my app to get revenue
    please help me.

  • jadson0102@live.com

    Hello, I need your help!
    If you can help me I will be very grateful!

    I am using this example to create an IAP and remove ads on my app,
    but
    does not work in the example do you do works but when I put the codes in my project it does not display anything.

    You can help me solve this problem?

  • todowindows

    Hello, nice tutorial, for banners works for me, how about removing an interstitialAd? Is that possible. I am using interstitialAd, so I want to know how to remove that ad as well, thanks a lot Vivek

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close