如何在iOS中获取应用内购买的本地货币价格?

30

我想查找用户所在的应用商店位置,这意味着他们在美国、澳大利亚(AUD)商店。以下是使用的代码。

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    Books = response.products;
    if ([SKPaymentQueue canMakePayments] && [response.products count]>0) {

    }

    for (SKProduct *book in Books) {
        NSLocale *locat=[NSLocale currentLocale];
        NSString *strlocate=[locat objectForKey:NSLocaleCountryCode];

        NSLog(@"\n\n\n Device country== %@ \n\n\n",strlocate);

        NSLocale* storeLocale = book.priceLocale;
        NSString *storeCountry = (NSString*)CFLocaleGetValue((CFLocaleRef)storeLocale, kCFLocaleCountryCode);
        NSLog(@"\n\n\n store country== %@ \n\n\n",storeCountry);
    }
}

如果书的定价为一级,则价格为:美元0.99澳元0.99日元85,基于应用商店价格矩阵表。

我在模拟器中进行了测试。我在系统中更改了iTunes,appstore和safari应用程序中的国家名称,但我只获取到了美国国家代码。

编辑: 设置->通用->国际->区域格式->国家

我在设备上进行了测试:只更改了设备国家。商店国家显示美国国家代码和价格。我想要在应用内购买之前存储国家代码。

根据货币波动,更改价格需要多少天? 是否提供任何API/查询来了解/更新波动的价格?

我的应用内操作由一个类完成。在执行应用内操作之前,我想向用户显示本地价格,同样价格也会反映在应用内操作中。

我显示免费和付费图书列表,这些列表是从我的服务器获取的。在其中,我显示每个项目的价格。处理应用商店时,这些项目价格将以美元价格显示。如果应用商店显示的价格也相同,我也想显示。因此,我想从我的服务器发送国家代码,以便我的服务器响应相关国家的价格列表和货币符号。


你尝试过更改设备的语言吗? - Mikael
更改设备语言,但没有用处。 - Senthilkumar
我也尝试过更改设备语言和地区,但没有成功。 - Inoka
请查看此链接:https://dev59.com/Vm855IYBdhLWcg3wSiQ7。它可能会对您有所帮助。 - arthankamal
请查看以下讨论串, https://dev59.com/Nm445IYBdhLWcg3wGWh7 - Banker Mittal
9个回答

33
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:prodcutIdentifier];
    productsRequest.delegate = self;
    
    // This will trigger the SKProductsRequestDelegate callbacks
    [productsRequest start];

这将调用您的委托函数。一旦上述操作完成,它将自动最终调用下面提到的函数。这是发送至应用商店请求最终确定的地方。

(void)requestDidFinish:(SKRequest *)request
{
   
    
    SKProduct *book = [Books objectAtIndex:0];
        
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:book.priceLocale];
    
    NSLocale* storeLocale = book.priceLocale;
    NSString *storeCountry = (NSString*)CFLocaleGetValue((CFLocaleRef)storeLocale, kCFLocaleCountryCode);
    
}

根据您的兴趣提供区域和国家详细信息。


我正在另一个视图控制器中执行此操作。那么我该如何在当前视图控制器中执行呢? - Senthilkumar
检查网络连接并在开始时获取值并将其存储在本地。因此,每次都要检查网络连接并执行该过程。只有这样,您才能显示最新的iTunes价格。 - Andro Selva
1
你能在不访问产品的情况下获取这个吗? - locoboy
4
在这个例子中,你什么时候会使用 numberFormatter 呢...? - Tulleb

28

这里的book是SKProduct:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:book.priceLocale];
NSString *formattedPrice = [numberFormatter stringFromNumber:book.price];

NSLog(@"%@", formattedPrice);

如果有任何不清楚的地方,请直接询问!

谢谢,


2
我想在开始应用内购买之前显示本地价格。 - Senthilkumar
请详细说明 "在应用内购买之前" 的意思,这段代码要求您仅请求产品,然后您可以从响应中获得产品的价格和所在地区,无需启动应用内购买流程! - Asif Mujteba
我的应用内操作创建了一个类。在应用内操作之前,我想要向用户显示本地价格,同时将同样的价格效果反映在应用内操作中。 - Senthilkumar
我展示了一份书籍列表,其中包括免费和付费的书籍,这个列表是从我的服务器获取的。在列表中,我显示了每个项目的价格。当处理App Store时,这些项目的价格将显示为美元价格。如果App Store显示的价格也是如此,那么我也想显示。因此,我想从我的应用程序向服务器发送国家代码,以便我的服务器响应与该国家相关的价格列表和货币符号。 - Senthilkumar
你不应该在服务器上保存产品价格,这是极其不鼓励的。只需在服务器上保存产品标识符,然后从服务器获取这些产品标识符,并从苹果服务器请求它们的详细信息,这种方法更好,可以节省很多麻烦!但如果你仍然想访问用户的应用商店国家代码,那么这里有一个非常好的答案 - Asif Mujteba
我试图在Xcode中使用此解决方案,但对 Books 的引用(如上文所述)导致错误 - ...未声明的标识符.... 已经尝试解决了几天了,让我发疯了。非常感谢任何帮助。 - Rob Slater

10

在Swift 3中:

    let numberFormatter = NumberFormatter()
    numberFormatter.formatterBehavior = .behavior10_4
    numberFormatter.numberStyle = .currency
    numberFormatter.locale = product.priceLocale
    let formattedPrice = numberFormatter.string(from: product.price)

10

一行代码:

productSKProduct 对象实例:

NSString *price = [NSString stringWithFormat:@"%@%@ %@", [product.priceLocale objectForKey:NSLocaleCurrencySymbol], product.price, [product.priceLocale objectForKey:NSLocaleCurrencyCode]];

结果:

$1.39美元


2
被踩了。这个标签对于美元和其他一些货币是正确的,但对于例如卢布来说是不正确的:1.39 р.。你应该始终使用 NumberFormatter - Vyachaslav Gerchicov

6
我使用这个:
  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:basicProduct.priceLocale];
    NSString *formattedString = [numberFormatter stringFromNumber:basicProduct.price];
    NSLog(@"product with currency:%@", formattedString);
    [retDict setObject:formattedString forKey:@"basic_price"];
    NSLog(@"Product title: %@" , basicProduct.localizedTitle);
    NSLog(@"Product description: %@" , basicProduct.localizedDescription);
    NSLog(@"Product price: %@" , basicProduct.price);
    NSLog(@"Product id: %@" , basicProduct.productIdentifier);

在委托方法中:

 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

这可以在用户点击开始购买之前完成。

5

您可以深入了解本地化对象,此外,我认为您也可以使用NSLocale提供的货币本地化功能,如下所示:

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

您将会得到正确格式的货币字符串,就像这样:
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];

我也在我的应用程序中尝试了这个。
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

希望这能对您有所帮助


3

在产品响应中,您将收到正确的产品价格和产品价格语言环境信息,该信息与您当前登录的AppStore账户相关。因此,如果您使用此语言环境格式化收到的价格,您将获得像“0.99$”,“33,0 руб。”等字符串。


-1
使用https://github.com/bizz84/SwiftyStoreKit,其中包含

SKProduct中的localizedPrice

直接使用此成员变量即可,无需任何代码转换!! App Store将根据当前用户的App Store国家/地区向您的应用程序响应正确的本地价格。如何更改:

https://apple.stackexchange.com/a/89186/283550

我已经测试了这个行为,当我将国家从香港改为台湾后,我可以看到价格等级1从8港元变成30新台币,现在是正确的。


2
看起来你有一个名为localizedPrice的SKProduct扩展,因为没有默认方法使用这个标题。如果是这样,你可以在这里提供这个扩展的代码。 - Artem Shmatkov
谢谢@zakhej,我刚刚检查了一下,它来自SwiftyStoreKit,我更新了答案,看看是否有帮助? - Amos

-4
create macro first then use it
#define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

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

混淆代码(这是宏),而且犯了很大的错误 - 如果一个日本人连接到美国商店并看到一个$20的应用内购买显示为20日元,他们会对你非常生气,并指责你欺诈。 - gnasher729
如果日本人想连接到我们的商店,他们将始终看到美元符号。 - mskw
我想表达的是,我同意gnasher的观点,即系统的语言环境与您连接的App Store不同。 - mskw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接