Random geekery since 2005.

Husband, father, programmer, speaker, writer, blogger, podcaster, collector, traveler, golfer.

This post is Day #23 in a series called the 31 Days of Windows Phone.

Yesterday, I wrote about how you could add your game to the Games hub on a Windows Phone.  Today, I’m going to show you how simple it is to set up your application to have Trial sections.  For example, let’s say you create a game that has 50 levels.  Perhaps you want the user to be able to play the first 5 levels for free, but to play the rest, they need to buy the game.  This article will show you how to do that.

Using the LicenseInformation Class

By adding the Microsoft.Phone.Marketplace assembly to our page, we have access to the LicenseInformation class, which is directly related to the “paid” status of our application.

using Microsoft.Phone.Marketplace;

Our next step is to actually use the LicenseInformation class, so let’s create an instance of it:

LicenseInformation li = new LicenseInformation();

Finally, LicenseInformation has a very nice Boolean property called IsTrial(), not surprisingly, that allows us to check if we’re in a Trial state or not.  You can use it in a simple if statement, like this:

if (!li.IsTrial())
{
	//Do something that only paid users can do.
}
else
{
	//Do something that all users, trial or paid, can do.
}

Testing Trial Mode

Unfortunately, there’s not a built-in mechanism to just swap between Trial and Paid states.  How I am handling this is pretty simple.  I’m using the same IF statement that you find in your App.xaml.cs file.  It checks to see if you’re debugging, and if you are, sets an IsolatedStorageSetting that I call “trialMode”.

Here’s the entire App() method, including the default code that is in our App.xaml.cs file.  In my example below, I am setting my trialMode variable to TRUE.  Turn this off when you want to test “paid” mode.

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

public App()
{
	// Global handler for uncaught exceptions. 
	UnhandledException += Application_UnhandledException;

	settings["trialMode"] = false;

	// Show graphics profiling information while debugging.
	if (System.Diagnostics.Debugger.IsAttached)
	{
		settings["trialMode"] = true;
				
		// Display the current frame rate counters.
		Application.Current.Host.Settings.EnableFrameRateCounter = true;

		// Show the areas of the app that are being redrawn in each frame.
		//Application.Current.Host.Settings.EnableRedrawRegions = true;

		// Enable non-production analysis visualization mode, 
		// which shows areas of a page that are being GPU accelerated with a colored overlay.
		//Application.Current.Host.Settings.EnableCacheVisualization = true;
	}

	// Standard Silverlight initialization
	InitializeComponent();

	// Phone-specific initialization
	InitializePhoneApplication();
}

Revisiting my code from earlier, I need to modify my IF statement to handle this new IsolatedStorageSettings value.  I am including my entire MainPage.xaml.cs file this time, so you can see everything in context.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Marketplace;
using System.IO.IsolatedStorage;

namespace Day23_UsingTrial
{
	public partial class MainPage : PhoneApplicationPage
	{
		LicenseInformation li = new LicenseInformation();
		IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
		
		// Constructor
		public MainPage()
		{
			InitializeComponent();

			if (!li.IsTrial()||(bool)settings["trialMode"] == false)
			{
				//Do something that only paid users can do.
			}
			else if (li.IsTrial() || (bool)settings["trialMode"] == true)
			{
				//Do something that all users, trial or paid, can do.
			}
		}
	}
}

That’s about all you need to do, though, and this is not necessarily the “best” way to handle this situation, but it’s been working for me.  If someone has a better way, I’d love to start using that instead.

Download the Code

To see all of this in a working example, download this solution file and play with it.  That’s always the best way to learn how stuff works.

download

Tags

4 responses to “31 Days of Windows Phone | Day #23: Providing Trial Versions of Your App”

  1. David Avatar
    David

    if (!li.IsTrial()||(bool)settings["trialMode"] == false)
    {
    //Do something that only paid users can do.
    }

    The second half of the boolean expression needs to be removed prior to deployment or it needs to be put in a guard cause that checks to see if we are in debug mode. I had this code in my app, the app would work well in the debugger but would crash in the Phone Emulator.

    The guard clause would look like this:
    if (System.Diagnostics.Debugger.IsAttached)
    {}

  2. David Avatar
    David

    Or better yet remove the "fake" trial test prior to deploying the app.

  3. Alfa Avatar
    Alfa

    how to make time trial apps?. Eg, the app expires after 7 days of usage.

    1. jeffblankenburg Avatar
      jeffblankenburg

      Timed trials aren’t recommended. When a user uninstalls an application, ALL of the data for that application also goes away, which means at the end of a timed trial, they could just uninstall/reinstall and be back in action.

      I recommend disabling the “save” feature, or limiting the functionality in some way. You could also show ads in the trial mode, and remove them when they pay for the app.

      Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: