以编程方式查找iphone的语言环境货币

问题描述 投票:54回答:7

我想以编程方式在用户的iPhone上找到货币区域设置。这意味着,如果用户在美国商店中,则货币区域设置应为USD,对于澳大利亚,则货币设置应为AUD。我此任务的目的是尝试将应用程序上列出的商品价格转换为与AppStore要求的价格几乎匹配。

例如,如果我们出售3美元的视频,而澳大利亚人想购买,那么我应该在应用屏幕上显示2.8澳元。它将使用户的计算量超过其所在国家/地区的实际价格。有人知道怎么做吗?

iphone objective-c localization currency
7个回答
127
投票

[在大多数情况下,货币符号是不够的。例如,在德国,我们将价格写成这样:1.99欧元,但在美国,人们则用1.99美元。字符串中有三个区别。货币符号,其位置和分隔符。

如果您想正确执行操作,则应使用NSNumberFormatter。它照顾了货币格式之间的所有差异。而且它比您做得更好。因为它适用于所有货币,而不仅仅是您要支持的4种主要货币。

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获得价格和priceLocale。

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

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];

编辑:因为您专门询问货币代码。

您可以使用NSString *currencyCode = [formatter currencyCode];来获得它。这将为您提供根据ISO 4217的货币代码。AUD,USD,EUR等。


46
投票

我使用这些键从语言环境中提取货币符号/代码

NSLocale *theLocale = [NSLocale currentLocale];
NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];

5
投票

我在应用程序中使用下面的代码来检索本地货币符号并找到分隔符。我会帮助您,

NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithString:@"50.00"];
NSNumberFormatter *currencyFormat = [[NSNumberFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[currencyFormat setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormat setLocale:locale];
NSLog(@"Amount with symbol: %@", [currencyFormat stringFromNumber:amount]);//Eg: $50.00
NSLog(@"Current Locale : %@", [locale localeIdentifier]);//Eg: en_US

谢谢。


5
投票
create macro first then use it
#define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);

3
投票

Matthias Bauch迅速回答:

var formatter = NSNumberFormatter()
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = product!.priceLocale
var currencyString = "\(formatter.stringFromNumber(product!.price)!)"

1
投票

感谢您的回答。我终于发现可以直接从Apple检索价格和货币代码:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {    
    NSArray *products = response.products;
    if (products && products.count != 0) {
        product = [products objectAtIndex:0];
        [[NSNotificationCenter defaultCenter] postNotificationName:PRICE_UPDATED object:product.LocalizedPrice];    
    } 

    // finally release the reqest we alloc/init’ed in requestProUpgradeProductData
    [productsRequest release];
}



@implementation SKProduct (LocalizedPrice)

- (NSString *)LocalizedPrice
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:self.priceLocale];
    NSString *formattedString = [numberFormatter stringFromNumber:self.price];
    [numberFormatter release];
    return formattedString;
}

@end

0
投票

这里是Swift 5中的一个示例:

let formatter = NumberFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency
formatter.locale = product.priceLocale
print(formatter.string(from: products![0].price)
© www.soinside.com 2019 - 2024. All rights reserved.