Friday, 10 April 2015

Coding the Trial Experience with Windows Phone 8

A couple of days ago I decided to make an app to access the Sinclair Infoseek service at World of Spectrum.  It’s going well so far, I have the basic search up and running, along with a display of the results.
ImageImage
I intend to add other features as and when I think of them. Current planned features include:
  • Favourites: Save your favourite searches
  • SkyDrive: Save the searches to SkyDrive
  • SkyDrive 2: When you have found an interesting game, you can pick the .tap/.tzx etc to upload to your SkyDrive account so that you can load it up in a ZX Spectrum emulator.  On Windows Phone I use MetroSpec. I might even attempt to open the game in MetroSpec directly from my app.
  • Save/Load Screens: Save and Load loading screens and in-game screens. Allow zooming etc.
  • A page that gets a random infoseek entry (maybe 30-50 at a time), so that you can browse through games that you may have never seen before, :)
  • More stuff that I cannot remember (why is it that I wake up at 3am with great ideas, then forget them by the time I get up in the morning…)
Now, back on-topic…
I would like to disable some features until the user has purchased the app (at the cheapest price that the Windows Phone App Store allows), so I had to be able to allow users to trial the app. I was worried that it might be a complicated process, but found that it was very, very easy.
Here are the steps I used:
  1. Add this line to your App.xaml.cs file
    using Microsoft.Phone.Marketplace;
  2. In the App class, declare the a local static variable which used the LicenseInformation class to determine if the app is running under a trial license or not
         private static LicenseInformation _licenseInfo = new LicenseInformation();
  3. Now you need a variable to cache whether the app is in Trial mode or not, so place this in yourApp.xaml.cs
            private static bool _isTrial = true;
            public bool IsTrial
            {
                get
                {
                    return _isTrial;
                }
            }
  4. Now for a method for checking the license. Again, this should go in App.xaml.cs
            private void CheckLicense()
            {
                // this displays a dialog so that we can simulate trial mode being on or off. 
    #if DEBUG
                string message = "Press 'OK' to simulate trial mode. Press 'Cancel' to run the application in normal mode.";
                if (MessageBox.Show(message, "Debug Trial",
                     MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    _isTrial = true;
                }
                else
                {
                    _isTrial = false;
                }
    #else
                _isTrial = _licenseInfo.IsTrial();
    #endif
            }
  5. We must check the trial status whenever the app is started or whenever it resumes from a sleeping state.  Once again, this code goes in App.xaml.cs
    private void Application_Launching(object sender, LaunchingEventArgs e)
            {
                CheckLicense();
            }
            private void Application_Activated(object sender, ActivatedEventArgs e)
            {
                CheckLicense();
            }
  6. To use this in your app, whenever you have code that should not be run when the user is in trial mode, you just use
    if ((Application.Current as App).IsTrial)
    {
        // Do this if app is in trial mode, the usual thing would be to inform the user
        // that the feature is no available in the trial and ask them to purchase the 
        // full app from the store.
    }
    else
    {
        // Do this if the app is *not* in trial mode
    }
  7. That’s all there is to it, easy huh. :)

No comments:

Post a Comment