Reputation: 979
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];
Upvotes: 6
Views: 2961
Reputation: 318934
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.
Upvotes: 6