Reputation: 4589
I'm trying to display prices for in-app purchases in multiple currencies. The user can choose 1 item, 2 items or 3 items at a time. The price updates it self at runtime(the user selects one item, than selects 2 for example), so that the user can see it being updated. This is fairly easy to accomplish if the price is displayed only in US dollars. When adding euros or British £ things start to get messy. How does one dynamically display IAP prices in multiple currencies?
Upvotes: 0
Views: 438
Reputation: 11892
Here's the function I use. Seems like some properties of SKProduct
are not filled, when you're in a non-supported store somewhere in the world.
internal func getPrice(#priceLocale: NSLocale!, price: NSDecimalNumber, count: Int) -> String? {
let formatter = NSNumberFormatter()
formatter.formatterBehavior = NSNumberFormatterBehavior.Behavior10_4
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.locale = priceLocale
return formatter.stringFromNumber(price * count)
}
This is the function I use (Actually as a property in extension SKProduct
)
It returns nil
, if something goes wrong with formatting count
or price
.
Upvotes: 1
Reputation: 26907
Something like this should work for you. The following code prints the localised title, currency code and the price.
let product = ... //your SKProduct
if let currencyCode = product.priceLocale.objectForKey(NSLocaleCurrencyCode) as? String {
print(product.localizedTitle + " : " + currencyCode + " \(product.price)")
}
Upvotes: 1