如何在 iOS 中获取应用内购买的区域设置货币价格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14453910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:56:36  来源:igfitidea点击:

How to get locale currency price for in-app purchases in iOS?

iphoneiosobjective-cin-app-purchase

提问by Senthilkumar

I want to find out user's App Store location. It means they are in US, Australia(AUD) store. Following code used.

我想找出用户的 App Store 位置。这意味着他们在美国,澳大利亚(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);
    }
}

I want like wise if book tier is one means US $ 0.99 AUD 0.99 JPY 85 based on the app store price matrix table.

我希望同样明智,如果图书层是基于应用商店价格矩阵表的一种,即 0.99 美元 0.99 日元 85 美元。

I tested in the simulator. I changed country name in iTunes, appstore and safari application in the system. but I get only US country code.

我在模拟器中测试过。我在系统中的 iTunes、appstore 和 safari 应用程序中更改了国家/地区名称。但我只得到美国国家代码。

Edit: Settings-> General-> International->Region Format-> country

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

I tested in the device: Device country only changed. store country show US country code and price. I want to store country code before start inapp purchase time.

我在设备中进行了测试:设备国家/地区仅更改。store country 显示美国国家代码和价格。我想在开始应用程序购买时间之前存储国家/地区代码。

how many days once change the price for based on currency fluctuation? is it provide any api/ query for fluctuated price to known/update?

根据货币波动改变价格多少天?它是否为已知/更新的波动价格提供任何 API/查询?

my inapp operation made one class. Before inapp operation made i want to display the local price to user same price effect reflect in the inapp operation.

我的 inapp 操作做了一节课。在inapp操作之前,我想向用户显示本地价格,在inapp操作中反映相同的价格效果。

i display the list of book free and paid books thats is list get from my server. in that i display the price for each item. those item price while processing the appstore it will show the dollar price. if appstore shown the price as also i want to shown. so that i want to send country code to my server from that my server respond that country related price list and currency symbol..

我显示了免费和付费书籍的列表,这是从我的服务器获取的列表。因为我会显示每件商品的价格。在处理应用商店时,这些商品价格会显示美元价格。如果 appstore 显示价格,我也想显示。所以我想将国家代码从我的服务器响应该国家相关的价格表和货币符号发送到我的服务器..

回答by Andro Selva

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

This will call your delegate function. Once the above is done, it will automatically call the below mentioned function finally. This is where the app store request sent will be finalized.

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

(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);
????
}

provide the Locale and country details as per your interest.

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

回答by Asif Mujteba

Here book is the SKProduct:

这里的书是 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);

simply ask if anything unclear!

有什么不清楚的就问吧!

Regards,

问候,

回答by mskw

One line of code:

一行代码:

productis the SKProductobject instance:

productSKProduct对象实例:

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

Results:

结果:

$1.39 USD

1.39 美元

回答by brnunes

In Swift 3:

在 Swift 3 中:

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

回答by Nikita P

i use this:

我用这个:

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

in delegate method:

在委托方法中:

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

this can be done before the user clicks to start buying stuff..

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

回答by Kirtikumar A.

You can go to know more on locale objects,also I think you also can use the currency locale by NSLocale ,as shown below

你可以去了解更多关于语言环境对象的信息,我认为你也可以通过 NSLocale 使用货币语言环境,如下所示

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

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

and i also tried this one in my app

我也在我的应用程序中尝试了这个

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: .00
NSLog(@"Current Locale : %@", [locale localeIdentifier]);//Eg: en_US

Hope It will help you

希望它会帮助你

回答by pcholberg

On device in productsResponse you receive correct product.price and product.priceLocale for AppStore account, you are currently logged in. So if you use this locale for formatting received price, you will get strings like "0.99$", "33,0 руб." and so on.

在 productsResponse 的设备上,您会收到 AppStore 帐户的正确 product.price 和 product.priceLocale,您当前已登录。因此,如果您使用此区域设置来格式化收到的价格,您将得到类似“0.99$”、“33,0 руб”的字符串.” 等等。

回答by Amos

Use https://github.com/bizz84/SwiftyStoreKit, there is

使用https://github.com/bizz84/SwiftyStoreKit,有

localizedPrice in SKProduct

SKProduct 中的本地化价格

Directly use this member variable is OK, NO ANY CODE CONVERSION IS NEEDED!!

直接使用这个成员变量就可以了,不需要任何代码转换!!

App Store will response the correct local price to your app, based on the current user's App Store Country/Region. How to change:

App Store 将根据当前用户的 App Store 国家/地区为您的应用提供正确的本地价格。如何改变:

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

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

I've tested the behaviour, it's correct after I changed the country from Hong Kong to Taiwan, I can see the price Tier 1 changed from HK$8 to $30. (Hong Kong Dollar to Taiwan Dollar)

我已经测试了行为,将国家从香港更改为台湾后是正确的,我可以看到第 1 层的价格从 8 港元变为 30 美元。(港元对台币)

回答by Hardik Darji

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

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