23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Trial Versions and App Purchase<br />

Implementing a trial version that hopefully leads to an app purchase is demonstrated in Scenario 1 of<br />

the Trial app and in-app purchase sample. When you run this sample and select Scenario 1, as shown in<br />

Figure 17-4, the simulator object is initialized using data/trial-mode.xml where the app’s IsActive and<br />

IsTrial elements are both set to true, meaning that we have a valid trial license. The ExpirationDate for<br />

this license is set to January 1, 2014, but we’ll play around with that in a moment.<br />

FIGURE 17-4 Scenario 1 of the Trial apps and in-app purchases sample (cropped slightly).<br />

The Trial Period button in this scenario just calculates the number of days remaining in the trial<br />

period, using basic arithmetic and the licenseInformation.expirationDate property. Again, let me<br />

point out that the proper way to do this is with the Windows.Globalization.Calendar class that we’ll<br />

see later in the “World Readiness and Localization” section and demonstrated in the Calendar details<br />

and math sample. Using the APIs designed for this purpose will insulate your app from regional<br />

variations.<br />

The Trial Mode and Purchased buttons just output different messages based on the state of the<br />

isActive and isTrial properties. Both button click handlers start like this:<br />

var licenseInformation = currentApp.licenseInformation;<br />

if (licenseInformation.isActive) {<br />

if (licenseInformation.isTrial) {<br />

What can make the output from these buttons more interesting is modifying the<br />

data/trail-mode.xml file with different initial values for IsActive and IsTrial. Also try setting the<br />

ExpirationDate to a time in the past (remembering that it’s UTC time, not local time), and you’ll see that<br />

IsActive automatically gets set to false. You can also try setting ExpirationDate about a minute in the<br />

future, set a breakpoint on the trailModeRefreshScenario function inside js/trial-mode.js, then run the<br />

sample again.<br />

774

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!