0

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)!)")
    }
 }
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 2
    Could this be a duplicate? https://stackoverflow.com/a/28907027/189804 – Adam Eberbach Jan 09 '18 at 00:54
  • in my app, I would simply do: `productInfo.localizedPrice`. I would reply as an answer ... but I haven't tested it – Gabriel Pires Jan 09 '18 at 01:05
  • I tried `productInfo?.priceLocale` but it prints `Price is: en_US@currency=USD (fixed)` – fs_tigre Jan 09 '18 at 01:15
  • 1
    @GabrielPires What is `localizedPrice`? There's no such property on `SKProduct`. – rmaddy Jan 09 '18 at 01:15
  • 2
    There is no `localizedPrice`; there is simply `price`, but the price is the price in the specified `priceLocale`, so someone with an account on the Italian store will get a price in Euros and the `priceLocal` property will reflect that. – Paulw11 Jan 09 '18 at 02:08
  • So, would you say that what I have is fine and should work if used with the proper account? – fs_tigre Jan 09 '18 at 02:14
  • Are you sure you want to sell a `$0.99 USD` in-app purchase for `CN¥0.99`? That's like `$0.15 USD`. It's not a matter of just changing the currency symbol and number formatting, you'll probably want to have currency-specific prices. – Alexander Jan 09 '18 at 02:37
  • 1
    *"So, would you say that what I have is fine and should work if used with the proper account?"* - your code will be correct once you set the formatter's `locale` to the `priceLocale` of the `SKProduct`. – rmaddy Jan 09 '18 at 03:12
  • 1
    One thing I believe is missing from both this question/comments and the dup linked to is *how* to test this. You can create sandbox users in iTunes Connect and, using a real device (I don't think you can use the simulator for this) **log** into the sandbox as these accounts and test out the purchase. As part of creating these sandbox users you can give them various locales (I like Japan, China, Canada, UK, and Germany) and you should see both the correct currency and values for the price tier that's set up. What I see missing in your code is `numberFormatter.locale = product.priceLocale`. –  Jan 09 '18 at 04:10
  • @rmaddy Ah .... I made a mistake. There is no localizedPrice property on SKProduct. I misinterpreted my code. I was using the localizedPrice in the SwiftyStoreKit pod. https://github.com/bizz84/SwiftyStoreKit/blob/master/SwiftyStoreKit/SKProduct+LocalizedPrice.swift. – Gabriel Pires Jan 09 '18 at 07:16
  • @dfd - Thank you for pointing out the fact that testing needs to be done with a sandbox with the appropriate App Store Territory assigned to the tester-user. I created a sandbox and assigned Italy as the App Store Territory and then login and tested it with that account and it worked, it now displays the right amount (€1,09). The one thing I couldn't figure out is how to add multiple locales as you pointed it out, I only see one option. – fs_tigre Jan 09 '18 at 12:53
  • 1
    You make multiple locales by making multiple sandbox users. :-) –  Jan 09 '18 at 13:08
  • The only issue is that you need an email for each user :-( – fs_tigre Jan 09 '18 at 13:19

0 Answers0