Reputation: 375
I can show the total yearly (or 6-month) auto renewable subscription price using displayPrice just fine, but I also want to show how much that would be per month, as I think its more enticing.
I could just use price and divide by 12 (or 6), but then I wouldn't know how to show the local currency symbol next to the price.
According to my googling this is easy to do in StoreKit 1 using SKProduct
, but how do I do it in StoreKit 2 using Product
?
Thanks!
PD: Is there a way to be able to test the different locales to make sure their prices/currency symbols are showing correctly?
Upvotes: 2
Views: 1410
Reputation: 1331
This worked well for me:
private func priceLabelPerMonth(for product: Product) -> String {
guard let subscription = product.subscription else { return product.displayPrice }
let numberOfUnits: Int
if subscription.subscriptionPeriod.unit == .year {
numberOfUnits = subscription.subscriptionPeriod.value * 12
}
else if subscription.subscriptionPeriod.unit == .month {
numberOfUnits = subscription.subscriptionPeriod.value
}
else {
numberOfUnits = 1
}
let pricePerMonth = product.price / Decimal(numberOfUnits)
let priceText = product.priceFormatStyle.format(pricePerMonth)
return priceText
}
Upvotes: 0
Reputation: 5250
We have own function that puts pieces together + you need to have Xcode updated to 15+:
Then it's just code stuff (disclaimer this is not Swift code, just for logic):
case 'MONTH':
return getFormattedPrice(Number(subscriptionVariant.price), subscriptionVariant.currency)
case 'YEAR': {
const perMonthValue = Number(subscriptionVariant.price) / 12
try {
const trunc = Math.floor(perMonthValue * 100) / 100
return getFormattedPrice(trunc, subscriptionVariant.currency)
...
Upvotes: 1
Reputation: 1876
It will be something like this,
let monthlyPriceDecimal = productItem.price.doubleValue/12
self.lblYearly.text = "\(productItem.priceLocale.currencySymbol ?? "") \(String(format: "%.2f", monthlyPriceDecimal)) / Month"
Upvotes: -1
Reputation: 624
Yeah, it is annoying that priceLocale
is not available on the SKProduct object in StoreKit 2. Apparently priceLocale
was removed in StoreKit 2 because it was not used very often!
To show your monthly price - you are correct - you will have to just use price
and divide by 12 or 6. That's what they say in their docs:
Use this property to perform arithmetic calculations with the price of the product. For a localized string representation of the price to display to customers, use the
displayPrice
property instead.
To know what to show the as local currency symbol next to the price, you will have to rely on priceFormatStyle
and then extract the currency
As for testing, the only thing I do is to have multiple sandbox testers created with different regions to see the prices displayed in their locale.
But know this - if you are using just displayPrice
- you have to worry about nothing. The price will always be in the locale of that App Store. These complications come in because you want to perform operations on your price (here - translating it to monthly) and then show it.
Upvotes: 2