SKProduct 信息上报可分为 价格、地区等基础信息、折扣优惠,以下详细介绍商品优惠信息
优惠分为两种类型
@property(nonatomic, readonly) SKProductDiscountType type API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2));
typedef NS_ENUM(NSUInteger, SKProductDiscountType) {
SKProductDiscountTypeIntroductory, // 推介优惠
SKProductDiscountTypeSubscription, // 促销优惠
} NS_SWIFT_NAME(SKProductDiscount.Type) API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2));
推介优惠
苹果会根据该苹果ID是否享受推介优惠,会自动下发该优惠信息,享受过的ID获取不到推介优惠信息,沙盒环境可以到后台清除沙盒账号购买记录重新购买测试
@property(nonatomic, readonly, nullable) SKProductDiscount *introductoryPrice API_AVAILABLE(ios(11.2), macos(10.13.2), watchos(6.2));
促销优惠
促销优惠为数组,可以获取到后台配置的促销优惠列表
@property(nonatomic, readonly) NSArray<SKProductDiscount *> *discounts API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2));
以上两种优惠都包含以下三种促销类型
@property(nonatomic, readonly) SKProductDiscountPaymentMode paymentMode API_AVAILABLE(ios(11.2), macos(10.13.2), watchos(6.2));
typedef NS_ENUM(NSUInteger, SKProductDiscountPaymentMode) {
SKProductDiscountPaymentModePayAsYouGo, // 随用随付
SKProductDiscountPaymentModePayUpFront, // 提前支付
SKProductDiscountPaymentModeFreeTrial // 免费试用
} NS_SWIFT_NAME(SKProductDiscount.PaymentMode) API_AVAILABLE(ios(11.2), macos(10.13.2), watchos(6.2));
商品信息上报
SKProductDiscountPaymentModePayAsYouGo 取周期方式区别于其他2种优惠,需特殊处理
+ (NSDictionary *)createProductParams:(SKProduct *) product {
if (!product) return nil;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setFormatterBehavior:NSNumberFormatterBehavior10_4];
[nf setNumberStyle:NSNumberFormatterCurrencyStyle];
[nf setLocale:product.priceLocale];
NSNumber *price = product.price;
NSString *currencyCode = nf.currencyCode;
NSString *countryCode = @"";
if (@available(iOS 10.0, *)) {
countryCode = [nf.locale countryCode];
}
NSString *symbol = [nf currencySymbol];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:product.productIdentifier ?: @"" forKey:@"product_id"];
[params setObject:price ?: @"" forKey:@"price"];
[params setObject:countryCode forKey:@"country_code"];
[params setObject:currencyCode ?: @"" forKey:@"currency_code"];
[params setObject:symbol ?: @"" forKey:@"symbol"];
if (@available(iOS 12.2, *)) {
NSMutableArray *promotions = [NSMutableArray array];
NSArray <SKProductDiscount *>* discounts = product.discounts;
for (SKProductDiscount *discount in discounts) {
[promotions addObject:[self productPromotion:discount]];
}
// 推介优惠
SKProductDiscount *introductoryPrice = product.introductoryPrice;
if (introductoryPrice) {
[promotions addObject:[self productPromotion:introductoryPrice]];
}
[params setObject:promotions forKey:@"promotion"];
}
return params.copy;
}
+(NSDictionary *)productPromotion:(SKProductDiscount *) discount {
if (@available(iOS 12.2, *)) {
// 促销价格
NSDecimalNumber *promotionPrice = discount.price;
// 促销周期
NSInteger periods = discount.subscriptionPeriod.unit;
// 优惠类型
NSInteger type = discount.type;
// 促销码
NSString *code = discount.identifier ?: @"";
// 促销期数
NSInteger count = discount.subscriptionPeriod.numberOfUnits;
if (discount.paymentMode == SKProductDiscountPaymentModePayAsYouGo) {
count = discount.numberOfPeriods;
}
// 促销类型
NSInteger mode = discount.paymentMode;
NSMutableDictionary *promotion = [NSMutableDictionary dictionary];
[promotion setObject:promotionPrice forKey:@"promotion_price"];
[promotion setObject:@(periods) forKey:@"promotion_period"];
[promotion setObject:@(type) forKey:@"discount_type"];
[promotion setObject:@(mode) forKey:@"promotion_type"];
[promotion setObject:code forKey:@"promotion_code"];
[promotion setObject:@(count) forKey:@"promotion_time"];
return promotion.copy;
}
return @{};
}
ps
优惠代码是无法拉到任何信息的
优惠数组是拉取的信息是优惠促销