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
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.
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.
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
- 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.