I am having an In-App-Purchase for an iPhone app.
I want to display the price in the users local currency in a UILabel. For this I need the price & currency in a variable.
How can I get the price including the currency using SKPayment? (If SKPayment is correct for this use.)
I'm instantiating the product using:
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"Identifier"];
Thank you all in advance for your feedback!
There's a problem with just using NSLocaleCurrencySymbol + price.stringValue: it doesn't handle the peculiarities of different locales, eg. whether they put the currency symbol in front or not. Norway, Denmark, Sweden and Switzerland all put their currency after, eg. 17.00Kr. Also, most(?) European countries use ',' instead of '.' for decimals, eg. "2,99 €" rather than "€2.99".
A better plan is to use NSNumberFormatter. The "priceLocale" that the SKProduct returned, as Ed demonstrated, is the key. It gives NSNumberFormatter the smarts to format a price correctly.
You can also make this a lot easier by adding a new property to SKProduct using an Objective-C category. Add the following two files to your project:
SKProduct+priceAsString.h:
#import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @interface SKProduct (priceAsString) @property (nonatomic, readonly) NSString *priceAsString; @end
SKProduct+priceAsString.m:
#import "SKProduct+priceAsString.h" @implementation SKProduct (priceAsString) - (NSString *) priceAsString { NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [formatter setLocale:[self priceLocale]]; NSString *str = [formatter stringFromNumber:[self price]]; [formatter release]; return str; } @end
Then, #import "SKProduct+priceAsString.h"
in your code, and you should just be able to use product.priceAsString
in code.