以编程方式查找iphone的区域设置货币 [英] Find locale currency for iphone programmatically

查看:119
本文介绍了以编程方式查找iphone的区域设置货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式查找用户iphone上的货币区域设置。这意味着,如果用户在美国商店,货币区域应该是美元,对于澳大利亚,它应该是澳元。我的目的是尝试将我们应用上列出的商品价格转换为几乎与AppStore要求的价格相匹配。

I want to find out the currency locale on user's iphone programmatically. That means, if user is in US Store, the currency locale should be USD, for Australia, it should be AUD. My purpose of this task is to try to convert the item price listed on our app to be nearly match with the price that AppStore ask.

例如,如果我们销售视频3美元,而澳大利亚人想购买它,那么我应该在我的应用程序屏幕上显示2.8澳元。它将减少用户对其国家实际价格的计算。有人知道怎么做吗?

For example, if we sell a video 3 usd, and an Australian wants to buy it, then I should show 2.8 AUD in my app screen. It will reduce the calculation in the user over the real price in his country. Does anybody know how to do it?

推荐答案

在大多数情况下,货币符号是不够的。例如,在德国,我们写这样的价格:1,99欧元,但美国人使用1.99美元。字符串有三个不同之处。货币符号,它的位置和分隔符。

In most cases the currency symbol won't be enough. For example, in Germany we write our prices like this: 1,99€ but people in the US use $1.99. There are three differences in the string. The currency symbol, the position of it and the separator.

如果你想做得对,你应该使用NSNumberFormatter。它负责货币格式之间的所有差异。它比你做得好得多。因为它适用于所有货币,而不仅仅是您想要支持的4种主要货币。

If you want to do it right you should use a NSNumberFormatter. It takes care of all the differences between currency formats. And it does it much better than you. Because it does it for all currencies, not just for the 4 main currencies you want to support.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];
NSString *localizedMoneyString = [formatter stringFromNumber:myCurrencyNSNumberObject];






如果您想在应用内购买此产品您不能依赖用户当前的区域设置,因为可以在具有DE(德语)区域设备的设备上使用基于美国的帐户。您的商品价格(德国实际价格为0.79欧元)将显示为0.99欧元(因为它在美国的价格为0.99美元)。这是错误的。您已从应用商店获得本地化价格,无需自行计算。

您可以获得每个SKProducts的价格和价格。


If you want to use this for in app purchase you can't rely on the users current locale, because it is possible to use a US-based account on a device with a DE (german) locale. And the price of your item (actual price is 0,79€ in Germany) would show as 0,99€ (because it costs $0.99 in the US). This would be wrong. You get a localized price already from the app store, there is no need to do calculations on your own.
And you get a price and a priceLocale for each of your SKProducts.

您将获得正确的格式化货币字符串,如下所示:

You would get the correct formatted currency string like this:

SKProduct *product = [self.products objectAtIndex:indexPath.row];
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:product.priceLocale];
currencyString = [formatter stringFromNumber:product.price];

编辑:因为您特意要求提供货币代码。

since you specifically asked for the currency code.

您可以使用 NSString * currencyCode = [formatter currencyCode]; 这将为您提供符合ISO 4217的货币代码。澳元,美元,欧元和等等。

You can get it with NSString *currencyCode = [formatter currencyCode]; This will give you the currency code according to ISO 4217. AUD, USD, EUR and so on.

这篇关于以编程方式查找iphone的区域设置货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆