Omar
Omar

Reputation: 979

Show in-app purchase price programatically

I have the following code added to my In App Purchase ViewController, but I want to show the price along with the text I have already added

My code for calling the price is:

 _priceFormatter = [[NSNumberFormatter alloc] init];
    [_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [_priceFormatter setLocale:product.priceLocale];

I want to call the price from that string after 'Upgrade for' text on my button

[buyButton setTitle:@"Upgrade for" forState:UIControlStateNormal];

How would I do this?

Upvotes: 0

Views: 156

Answers (2)

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:product.priceLocale];

NSString* titleWithPriceAndCurrencySign = [formatter stringFromNumber:product.price];

[buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", titleWithPriceAndCurrencySign] forState:UIControlStateNormal];

Sometimes you may want to customize standard price representation for some locale. For example, in russian locale prices are presented with redundant zeros - 33,00 RUB. I'd like to have a short notation - 33 r.. So I add following code:

if ([product.priceLocale.localeIdentifier hasSuffix:@"currency=RUB"])
{
    [formatter setMaximumFractionDigits:0];
    [formatter setCurrencySymbol:@"r."];
}

Upvotes: 0

tsmith1024
tsmith1024

Reputation: 51

Maybe I'm missing something here (haven't tested), but is there a reason something like this won't work?

NSString *buttonTitleString= [NSString stringWithFormat:@"Upgrade for %@", priceString];
[buyButton setTitle:buttonTitleString forState:UIControlStateNormal];

Or, to do it in one line

[buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", priceString] forState:UIControlStateNormal];

where priceString is your string for the price, likely using the formatter you described in the question.

Upvotes: 1

Related Questions