30

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

- (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.

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.

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.

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

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.

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..

emotality
  • 12,795
  • 4
  • 39
  • 60
Senthilkumar
  • 2,471
  • 4
  • 30
  • 50

9 Answers9

33
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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • this operation am doing in the another viewcontroller. so how can i do in the current view controller? – Senthilkumar Jan 31 '13 at 09:03
  • Check for the internet connection and fetch the values at the beginning and store it locally. So each time check for internet connection and do the process. Only then you will be able to show the latest itunes price. – Andro Selva Jan 31 '13 at 09:06
  • 1
    can you get this without accessing the product? – locoboy Oct 31 '14 at 23:53
  • 4
    When are you using numberFormatter in this example...? – Tulleb Sep 12 '16 at 15:12
28

Here book is the 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,

Asif Mujteba
  • 4,596
  • 2
  • 22
  • 38
  • 2
    i want before start inapp purchase.. to display the local price – Senthilkumar Jan 25 '13 at 10:16
  • please elaborate what do you mean by "before inapp", this code requires you to only request products and you can get product's prices and locales from response, no need to start inapp purchase process! – Asif Mujteba Jan 25 '13 at 10:22
  • 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. – Senthilkumar Jan 25 '13 at 10:28
  • 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.. – Senthilkumar Jan 25 '13 at 11:24
  • You shouldn't save product price on your server, thats extremely discouraged, just save product identifiers on your server, then get those product identifiers from server and request their details from apple server, this approach is much better and would save you much hassle! but if you still want to access user's app store country code than [here is very good answer](http://stackoverflow.com/questions/11670302/ios-itunes-store-country) – Asif Mujteba Jan 25 '13 at 14:34
  • I am trying to use this solution in Xcode, but reference to Books (above) results in an error - ...undeclared identifier.... Been trying to resolve this for days, driving me mad. Any help much appreciated. – Rob Slater Nov 15 '21 at 08:20
10

One line of code:

product is the SKProduct object instance:

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

Results:

$1.39 USD

emotality
  • 12,795
  • 4
  • 39
  • 60
mskw
  • 10,063
  • 9
  • 42
  • 64
  • 2
    downvoted. This label is correct for USD and some other currencies but incorrect for example for RUB: `1.39 р.`. You should always use `NumberFormatter` – Vyachaslav Gerchicov May 02 '17 at 13:44
9

In Swift 3:

    let numberFormatter = NumberFormatter()
    numberFormatter.formatterBehavior = .behavior10_4
    numberFormatter.numberStyle = .currency
    numberFormatter.locale = product.priceLocale
    let formattedPrice = numberFormatter.string(from: product.price)
brnunes
  • 1,943
  • 26
  • 30
6

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..

Nikita P
  • 4,226
  • 5
  • 31
  • 55
5

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

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

Hope It will help you

Kirtikumar A.
  • 4,140
  • 43
  • 43
3

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.

pcholberg
  • 520
  • 2
  • 17
-1

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

localizedPrice in 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:

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)

Amos
  • 2,222
  • 1
  • 26
  • 42
  • 2
    Looks like you have some extension for SKProduct named localizedPrice, because there is no default method with such title. If so you could provide here the code from this extension. – Artem Shmatkov Dec 06 '18 at 12:57
  • thanks @zakhej, just checked it comes from SwiftyStoreKit, updated the answer, see if it helps? – Amos Dec 07 '18 at 01:46
-4
create macro first then use it
#define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);
Hardik Darji
  • 3,633
  • 1
  • 30
  • 30
  • Obfuscating (that's the macro), and badly wrong - if a Japanese person is connected to a US store and sees a $20 in app purchase displayed as 20 Yen, they will be _very_ angry with you and accuse you of fraud. – gnasher729 Sep 11 '15 at 15:12
  • If Japanese wants to connect to us store, they will always see dollar sign. – mskw Aug 14 '16 at 17:58
  • I meant to agree with gnasher in that the locale of the system is different than the App Store you are connected to – mskw Aug 14 '16 at 18:01