Reputation: 61
It means that when people is going to buy something in my game , and I should show them the price of the object first before they click the "buy" button , but in different countries we need use different currency , if the user is in USA , he should see the price is $0.99 , and in another country I should show them their local currency with the price , so how can I do it in code in my project . I am sorry , but I am new to IOS , so really hope to get your help , thank you so much
Upvotes: 2
Views: 2511
Reputation: 6969
Here is an Objective C solution for your needs - you can include Objective C Header into your swift project and use it.
- (NSString *) getLocalizedCurrencyString : (NSNumber *) amount :(NSLocale *)priceLocale
{
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setLocale:priceLocale];
[currencyFormatter setMaximumFractionDigits:2];
[currencyFormatter setMinimumFractionDigits:2];
[currencyFormatter setAlwaysShowsDecimalSeparator:YES];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *localizedCurrency = [currencyFormatter stringFromNumber:amount];
return localizedCurrency;
}
Usage:
let priceLocale: NSLocale = product.priceLocale as NSLocale
let price: NSString = IAPHelper.sharedInstance().getLocalizedCurrencyString(product.price, priceLocale)
where IAPHelper
is the Objective C class holding IAP code.
Upvotes: 1
Reputation: 2184
Once you get your products from Apple, you can get the price
in the local currency from the SKProduct
- https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/#//apple_ref/occ/instp/SKProduct/price - that you can then display to the user in your purchase interface.
More details (including a code sample with formatting): http://bendodson.com/weblog/2014/12/10/skproduct-localized-price-in-swift/
Upvotes: 2