应用内购买演示 - 基类
以下基类属于应用内购买演示:
类
| 类 | 描述 |
|---|---|
| InAppProduct | 在商店中注册的产品。 |
| 应用内商店 | 管理应用内购买的主要入口。 |
| 应用内交易 | 包含关于外部应用商店中某笔交易的信息。 |
| InAppPurchaseBackend | 与外部商店进行通信。 |
InAppProduct
InAppProduct 在产品已在 InAppStore 中注册并确认存在后,封装外部应用商店中的该产品。它具有一个与外部应用商店中该产品标识符相匹配的标识符,一个从外部应用商店获取的价格,以及一种产品类型。
产品类型可以是“Consumable ”或“Unlockable ”。消耗型产品可以购买任意次数,但每次交易都必须由应用程序显式完成。解锁型产品只能购买一次。
InAppProduct 拥有 5 个可返回变量:price 、title 、description 、identifier 和productType 。除 productType 外,其余均返回QString 。
ProductType 是一个枚举类型,如果是 Consumable,则返回0 ;如果是 Unlockable,则返回1 。
enum ProductType
{
Consumable,
Unlockable
};请查看其派生类:Android 端的 `AndroidInAppProduct ` 和 iOS 端的 `IosInAppProduct `。
InAppStore
管理应用内购买的主要入口。它是AndroidInAppProduct 和IosInAppProduct 的基类。
InAppStore 用于以跨平台的方式管理应用程序中的应用内购买。根据编译器的不同,InAppStore 会通过Macros 检查可用的平台。
void InAppStore::setupBackend()
{
#ifdef Q_OS_ANDROID
d->backend = new AndroidInAppPurchaseBackend;
#endif
#ifdef Q_OS_IOS
d->backend = new IosInAppPurchaseBackend;
#endif
d->backend->setStore(this);
...初始化商店
在演示程序中进入商店页面时,InAppStore 会在 setupBackend() 函数中将所有信号连接到相关槽(slot)。随后,它使用 registerProduct() 函数,将外部商店中注册的每个产品的 productID 和 productType 注册到QHash 的 registeredProducts 中。
产品注册是异步进行的,当从外部商店找到该产品时,会在某个时刻触发 productRegistered() 信号。
void InAppStore::registerProduct(InAppProduct *product)
{
d->registeredProducts[product->identifier()] = product;
emit productRegistered(product);
}完成购买
一旦商品在商店中成功注册,用户即可通过点击应用商店页面上的商品进行购买。QML 将调用 AndroidInAppProduct 或IosInAppProduct 中的 product.purchase() 函数,从而启动外部商店的购买流程。
当购买完成时(无论是否成功),都会向InAppProductQmlType 发送transactionReady信号,以通知QML开始最终确认交易。
恢复购买
在演示中,可解锁的购买项目将保存在应用存储中。清除存储后,用户将失去该可解锁的购买项目,且无法再次购买,因为根据外部商店的记录,该项目已被拥有。
您可以在应用商店页面中使用“restore purchases ”按钮来恢复已解锁的购买项目。该恢复购买按钮会调用 restorePurchases() 函数,并检查外部商店中已拥有的购买项目。它会发出 transactionRedy() 信号以完成并恢复购买。
InAppStore 没有派生类。
InAppTransaction
InAppTransaction 包含外部应用商店中交易的相关信息,通常是在调用 InAppProduct::purchase() 后返回的结果。 当用户完成购买流程(例如通过输入密码确认购买)后,包含该产品的 InAppStore 实例将发出带有交易数据的 InAppStore::transactionReady() 信号。
status() 函数提供交易是否成功的信息。如果成功,则应用程序应采取相应措施。在执行完必要的操作后,应调用 finalize()。无论交易状态如何,都应调用 finalize() 函数。
请查看针对 Android 的派生类AndroidInAppTransaction 以及针对 iOS 的派生类IosInAppTransaction 。
InAppPurchaseBackend
InAppPurchaseBackend 用于为AndroidInAppPurchaseBackend 和IosInAppPurchaseBackend 创建派生类。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.