- Introducing the Store API and Simulator
- Simulating and Testing Trial Functionality
Simulating and Testing Trial Functionality
When developers release trial versions of their software, they usually limit ether the time that a user can preview their full-featured app, or they leave the trial duration open-ended, but they disable certain key features until after a purchase is made. In Win8 style apps, you’re free to take either approach, but these choices do lead down different paths for development, so you should consider which to take as early as possible so that you can plan how to deal with an expired trial (in the case of time-limited trials) or the disabling of key features (in the case of feature-limited trials).
For both cases, the interaction with the Store API is the same, and we’ll take a look at that now. For this example, I’ve decided to modify the ads I added to my Tom8to application in the last example, and only show them to users who are running a trial version of the app. The absence of ads can be considered a feature, so this is a feature-limited trial. As long as the user doesn’t mind the ads, they’re free to use the app in trial mode as long as they like. I’ll start by adding some of the Store API initialization logic to my startup code for the app.
function initLicense() { //var currentApp = Windows.ApplicationModel.Store.CurrentApp; // REPLACE AT PUBLICATION TIME var currentApp = Windows.ApplicationModel.Store.CurrentAppSimulator; return currentApp.licenseInformation; } licenseInfo = initLicense();
Because my app is not yet in the store, it has no appId, and the preceding code will get a reference to the app simulator object. I can then then grab the license information, determine if the user is running in trial mode and, if so, initialize an ad control.
if (licenseInfo.isTrial) { ad = document.querySelector('#adsBlock'); var adControl = new MicrosoftNSJS.Advertising.AdControl(ad, { applicationId: "test_client", adUnitId: "ImageText_320x50" }); }
The isTrial Boolean tells me whether my user owns a license for my app. If they don’t, I’ll show them an ad, and the app will continue to behave as shown previously in Figure 13.20. If they do own a license, then I shouldn’t show any ads, and I can test this by changing the IsTrial child element of the LicenseInformation element in my WindowsStoreProxy file.
<LicenseInformation> <App> <IsActive>true</IsActive> <IsTrial>false</IsTrial> </App> <Product ProductId="AlarmPack1"> <IsActive>false</IsActive> </Product> </LicenseInformation>
Now, if I run the app again, isTrial will return false, and the ad won’t display.
Alternatively, if I wanted to offer a time-limited trial to my users, instead of a feature-limited trial, I might also want to use the CurrentAppSimulator for testing.
var dtFormatting = Windows.Globalization.DateTimeFormatting; if (licenseInfo.isTrial) { var longDateFormat = dtFormatting.DateTimeFormatter("longdate"); var days = (licenseInfo.expirationDate - new Date()) / 86400000; var msg = Windows.UI.Popups.MessageDialog("You have " + Math.ceil(days) + " days left in your trial"); msg.commands.append(new Windows.UI.Popups.UICommand("Buy Now", sendToStore)); msg.commands.append(new Windows.UI.Popups.UICommand("Close")); msg.showAsync(); } function sendToStore() { var uri = new Windows.Foundation.Uri(currentApp.linkUri.rawUri); Windows.System.Launcher.launchUriAsync(uri); }
In this example, I use the expirationDate property on the licenseInfo object to determine the days remaining in the user’s trial, and then I show them a popup with a gentle reminder of the days left in their trial, along with a helpful “Buy Now” button that will open Win8 IE to my app’s landing page in the store. If I run this code, as-is, I’m going to see some astronomical number of days left since the simulator doesn’t have a trial period set and a default date in the year 9999 is chosen instead. As before, I can fix this with the WindowsStoreProxy file, this time by adding an ExpirationDate element to my LicenseInformation parent:
<LicenseInformation> <App> <IsActive>true</IsActive> <IsTrial>true</IsTrial> <ExpirationDate>2012-07-31T23:59:59.00Z</ExpirationDate> </App> <Product ProductId="AlarmPack1"> <IsActive>false</IsActive> </Product> </LicenseInformation>
Now, when I run my app, I’ll see a more reasonable date, as shown in Figure 13.23.
Figure 13.23 Working with trial expiration dates
If the user does choose to buy my app when prompted, I’ll need to deal with a license change accordingly if my app is still running or is resumed once the user completes a purchase. I can do so by subscribing to the licensechanged event of the licenseInfo object.
licenseInfo.addEventListener('licenseChanged', function () { if (!licenseInfo.isTrial) { WinJS.UI.Animation.fadeOut(ad); } });
In this case, when the event fires, I’ll re-query the licenseInfo object and, if the user is no longer running in trial mode, I’ll fade out the ad.