What I'm trying to do is display the total cost of an in-app purchase when a button is tapped. For instance, one of my in-app purchases costs $0.99
US dollars, but what I want is to display the proper amount if someone is buying it outside the US. For instance, if someone is buying it in Italy I want them to see the equivalent of the .99 cents, which in this case it would be €0.84
The following code only shows and formats the 0.99, it does not do the conversion to the current currency.
What am I missing?
What is the proper way to display the cost of an in-app purchase?
@IBAction func showPrice(){
var productInfo:SKProduct?
for product in IAPManager.sharedInstance.products{
productInfo = product as? SKProduct
let numberFormatter = NumberFormatter()
numberFormatter.formatterBehavior = .behavior10_4
numberFormatter.numberStyle = .currency
let inAppPrice = numberFormatter.string(from: productInfo!.price)
print("Price is: \((inAppPrice)!)") // output for US - Price is: $0.99
// output for China - Price is: CN¥0.99
// output for Italy - Price is: €0,99
}
}
EDIT: Based on the documentation my code should work.
EDIT: Just for reference, here is the working code: Thanks to @rmaddy
and @dfd
, .priceLocale
and testing in a sandbox is the key.
@IBAction func showPrice(){
var productInfo:SKProduct?
for product in IAPManager.sharedInstance.products{
productInfo = product as? SKProduct
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = productInfo!.priceLocale
let inAppPrice = numberFormatter.string(from: productInfo!.price)
print("Price is: \((inAppPrice)!)")
}
}