Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Localized price for In App Purchase

I am using the following code in My ViewController, however when I call _priceFormatter it displays the price as (null)

 [buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", [_priceFormatter stringFromNumber:product.price]] forState:UIControlStateNormal];

ViewController.m

{


    NSNumberFormatter * _priceFormatter;


}

ViewDidLoad

    [RageIAPHelper sharedInstance];

    _products = nil;

    [[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
        if (success) {
            _products = products;

        }
    }];

    SKProduct * product = (SKProduct *) [_products objectAtIndex:0];


    ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]);

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

    [_priceFormatter stringFromNumber:product.price];

EDITED

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

NSString *priceString = [_priceFormatter stringFromNumber:product.price];

Button:

    UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake(-1, 370, 320, 60)];
[buyButton setTitle:[NSString stringWithFormat:@"Upgrade for %@", priceString] forState:UIControlStateNormal];
    [buyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
    [buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buyButton.titleLabel setShadowColor:[UIColor colorWithWhite:0.1 alpha:1.0]];
    [buyButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
    [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    buyButton.tag = 0;
    [[self view] addSubview:buyButton];
like image 854
Omar Avatar asked Jan 20 '14 20:01

Omar


People also ask

How do you find out the cost of in-app purchases?

In the App Store, applications with in-app purchases will have a disclaimer next to the purchase button. If the app is initially free, this note should be beside the Get button. If it's paid, the disclaimer is beside the price tag.

Does In-app purchase mean there is a charge?

An in-app purchase is any fee an app may ask for. Many in-app purchases are optional or give users additional features. Others serve as subscriptions and require users to sign up and pay a fee to use the app – often after an initial free trial.

How do you know if an app is free or how much it costs?

While installing the application from Google Play Store you see a green ‘Install’ button on the app, which means that the app is free, though it might include some in-app purchases, as per the features. On the other hand, when the green button has a cost marked on it, it means that it is a paid app.


1 Answers

You need to wait until the completion handler is called to process the products:

[RageIAPHelper sharedInstance];
_products = nil;

[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
    if (success) {
        _products = products;

        SKProduct * product = _products[0];

        [[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier];

        _priceFormatter = [[NSNumberFormatter alloc] init];
        [_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        [_priceFormatter setLocale:product.priceLocale];
        NSString *price = [_priceFormatter stringFromNumber:product.price];
        someLabel.text = price;
    }
}];

And as you can see, you need to actually make use of the formatted number string. You were just throwing away the value.

like image 100
rmaddy Avatar answered Oct 14 '22 02:10

rmaddy