StoreKit2结构体Product
StoreKit2结构体Product

StoreKit2结构体Product

Product 是来自 StoreKit 2 的结构体(Swift 原生),表示一个 App Store 中的内购商品(In-App Purchase),Apple 框架提供的。

如何查看 Product 的字段属性?

不能直接print(product) 输出所有属性是因为:

Product 并不遵循 CustomStringConvertible 或 Encodable,所以 print() 不会打印结构体详情;

它的大部分属性是 async 的(需异步访问),不能在普通 print 中一览无余;

是一个 Swift 原生类型,不支持像字典那样直接 dump 全部内容。

Apple 官方公开的主要属性(StoreKit 2)

可以使用的 Product 属性主要包括:

1、id:String类型,商品的唯一标识符(Product ID),这里和App Store Connect设定的id一致。

2、displayName:String类型,商品的展示名称。

3、description:String类型,商品描述。

4、displayPrice:String类型,本地化后的价格字符串(例如 ¥6.00)。

5、price:Decimal类型,原始价格。

6、priceFormatStyle:FormatStyle<Decimal>类型,用于格式化价格的样式。

7、subscription:Product.SubscriptionInfo?类型,如果是订阅商品,包含订阅相关信息。

8、type:Product.ProductType类型,商品类型(consumable、non-consumable、auto-renewable 等)。

9、isFamilyShareable:Bool类型,是否支持家庭共享。

10、isDownloadable:Bool类型,是否可以下载内容(主要针对 macOS)。

查看某个商品的属性

if let product = iapManager.products.first {
    print("商品 ID: \(product.id)")
    print("商品名称: \(product.displayName)")
    print("商品描述: \(product.description)")
    print("价格: \(product.price)")
    print("本地化价格: \(product.displayPrice)")
    print("商品类型: \(product.type)")
}
   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注