如何创建应用的试用版 (HTML)

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

如果客户可在试用期内免费使用你的应用,你可以将应用设计为在试用期去除或限制使用某些功能。也可以在客户购买你的应用之前,启用仅在试用期才会出现的某些功能,如横幅或水印。 让我们看看如何在应用中添加此功能。

如果你想尝试使用应用试用版和其他重要 Windows 8 功能,请下载 Windows 8 实例实验室。这些实验室提供用于以你选择的编程语言(JavaScript 和 HTML 或 C# 和 XAML)创建示例 Windows 应用商店应用的模块化分步说明。

你需要了解的内容

技术

先决条件

  • 要修改的 Windows 运行时应用

说明

步骤 1: 选取要在试用期内启用或禁用的功能

应用的当前许可证状态存储为 LicenseInformation 类的属性。通常,将那些取决于许可证状态的功能放在我们下一步介绍的条件块中。在考虑这些功能时,确保实现该功能的方式允许这些功能在所有许可证状态下均能正常工作。

另外,决定你希望在应用运行时如何处理对应用许可证的更改。你的试用版可以是全功能的,但具有付费版所没有的应用内广告横幅。或者,你的试用应用可以禁用某些功能,或定期显示消息要求用户购买应用。

考虑你正设计的应用类型,什么是适合它的试用或到期策略。对于试用版的游戏,一个好的策略是限制用户可以玩的游戏内容量。对于试用版的实用工具,可能需要考虑设置一个到期日期,或限制潜在购买者可以使用的功能。

对于大部分非游戏应用,设置一个过期日期很有用,因为用户可很好地理解整个应用。以下是一些常见的过期场景和处理它们的选项。

  • 试用许可证在应用正在运行时过期。

    如果应用正在运行时试用许可证过期,应用可以:

    • 不执行任何操作。
    • 向客户显示一条消息。
    • 关闭。
    • 提示客户购买应用。

    最佳做法是显示一条消息,提示客户购买应用,如果客户购买它,则继续启用所有功能。如果用户决定不购买应用,则关闭它或定期提醒他们购买应用。

  • 在应用启动前试用许可证过期。

    如果在应用启动前试用许可证过期,应用将不会启动。相反,用户将看到一个对话框,该对话框为用户提供从应用商店购买你的应用的选项。

  • 客户在应用运行时购买它

    如果客户在应用运行时购买它,以下是应用可执行的一些操作。

    • 不执行任何操作,让他们继续在试用模式下操作,直到重新启动应用。
    • 感谢他们购买,或者显示一条消息。
    • 静默地启用在完整许可证下可用的功能(或禁用仅限试用的通知)。

如果希望检测许可证更改并在应用中执行某种操作,你必须为此添加一个事件处理程序,如下一步中所述。

步骤 2: 初始化许可证信息

在你的应用初始化时,获取你的应用的 LicenseInformation 对象,如该示例所示。我们假设 licenseInformationLicenseInformation 类型的全局变量或字段。

初始化 CurrentAppCurrentAppSimulator 以访问应用的许可证信息。


function initializeLicense()
{
    // (some app initialization functions)

        // Initialize the license info for use in the app that is uploaded to the Store.
        // uncomment for release
        // currentApp = Windows.ApplicationModel.Store.CurrentApp;

        // Initialize the license info for testing.
        // comment the next line for release
        currentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;

        // get the license info
        licenseInformation = currentApp.licenseInformation;

    // (other app initialization functions)
}

添加事件处理程序,以便许可证在应用运行的情况下发生更改时接收通知。如果在试用期到期或客户通过应用商店购买应用等情况下,应用的许可证将发生更改。


function initializeLicense()
{
    // some app initialization functions

        // Initialize the license info for use in the app that is uploaded to the Store.
        // uncomment for release
        // currentApp = Windows.ApplicationModel.Store.CurrentApp;

        // Initialize the license info for testing.
        // comment the next line for release
        currentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;

        // Get the license info
        licenseInformation = currentApp.licenseInformation;

        // Register for the license state change event.
        licenseInformation.addEventListener("licensechanged", reloadLicense);

    // other app initializations function
}

function reloadLicense()
{
    // (code is in next steps)
}

步骤 3: 将功能编码到条件块中

在引发许可证更改事件时,你的应用必须调用许可证 API 以确定试用状态是否已发生更改。本步骤中的代码显示如何构造此事件的处理程序。此时,如果用户购买了应用,则向用户提供许可状态已发生更改的反馈是一个好做法。你可能需要请求用户重新启动应用(如果你已经这样编码)。但是一定要让这一过渡尽可能无缝和轻松地进行。

此示例显示如何评估应用的许可证状态,以便你可以相应启用或禁用应用的功能。


function reloadLicense()
{
    if (licenseInformation.isActive) 
    {
        if (licenseInformation.isTrial)
        {
            // Show the features that are available during trial only.
        }
        else
        {
            // Show the features that are available only with a full license.
        }
    }
    else
    {
        // A license is inactive only when there's an error.
    }
}

步骤 4: 获取应用的试用到期日期(仅限 Windows)

包含用于确定应用的试用到期日期的代码。

此示例中的代码定义一个函数来获取应用试用许可证的过期日期。如果许可证仍然有效,则显示到期日期及试用到期之前剩余的天数。

function displayTrialVersionExpirationTime()
{
    if (licenseInformation.isActive)
    {
        if (licenseInformation.isTrial)
        {
            var longDateFormat = Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longdate");

            // Display the expiration date using the DateTimeFormatter. 
            // For example, longDateFormat.format(licenseInformation.expirationDate) 

            var daysRemaining = (licenseInformation.expirationDate - new Date()) / 86400000;

            // Let the user know the number of days remaining before the feature expires.
        }
        else
        {
            // ...
        }
    }
    else
    {
       // ...
    }
}

步骤 5: 使用对许可证 API 的模拟调用测试功能

现在,使用对许可证服务器的模拟调用测试你的应用。在 JavaScript、C#、Visual Basic 或 Visual C++ 中,在应用的初始化代码中将对 CurrentApp 的引用替换为 CurrentAppSimulator

CurrentAppSimulator 从称为“WindowsStoreProxy.xml”的 XML 文件(位于 %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData)中获取测试特定的许可信息。如果此路径和文件不存在,则必须在安装或运行时期间创建它们。如果尝试在 WindowsStoreProxy.xml 未出现在特定位置的情况下访问 CurrentAppSimulator.LicenseInformation 属性,则会出现错误。

此示例说明如何将代码添加到你的应用,以在不同的许可状态下测试它。

function appInit
{
    // some app initialization functions

        // Initialize the license info for use in the app that is uploaded to the Store.
        // uncomment for release
        // currentApp = Windows.ApplicationModel.Store.CurrentApp;

        // Initialize the license info for testing.
        // comment the next line for release
        currentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;

        // Get the license info
        licenseInformation = currentApp.licenseInformation;

    // other app initialization functions
}

你可以编辑 WindowsStoreProxy.xml 以更改你的应用及其功能的模拟到期日期。测试所有可能的到期和许可配置,以确保任一方面都按预期运行。

步骤 6: 将模拟的许可证 API 方法替换为实际 API

在使用模拟的许可证服务器测试你的应用后,以及向应用商店提交应用进行认证前,将 CurrentAppSimulator 替换为 CurrentApp,如下一个代码示例所示。

要点  将应用提交到应用商店时,你的应用必须使用 CurrentApp 对象,否则认证将失败。

 

function appInit
{
    // (some app initialization functions)

        // Initialize the license info for use in the app that is uploaded to the Store.
        // uncomment for release
        currentApp = Windows.ApplicationModel.Store.CurrentApp;

        // Initialize the license info for testing.
        // comment the next line for release
        //   currentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;

        // Get the license info
        licenseInformation = currentApp.licenseInformation;

    // (other app initialization functions)
}

步骤 7: 描述免费试用版对客户的工作方式

确保解释了你的应用在免费试用期及之后的行为,使客户不会对应用的行为感到惊讶。

有关描述应用的更多信息,请参阅你的应用提要

相关主题

试用版应用和应用内购买示例

CurrentApp

CurrentAppSimulator