vendredi 27 mars 2015

Obtaining a complete NSString from one class to another for the title of a UIButton

I have a simple application and I am now implementing In App Purchases. The layout of the problem at hand is simple:



  • I have a IAPViewController which contains the elements like a UIButton and UILabel

  • I have a e100EntriesPurchase which is what is doing the actual IAPs.


When the user clicks on an element like a UIButton in the IAPViewController, it invokes a method in the e100EntriesPurchase class which shows a UIAlertView to the user. The otherButtonTitle has a formatted text to show the price in your local country.



- (void)displayStoreUI
{
// Obtain formatted price
[self obtainPurchasePricing];

NSLog(@"HERE THE PRICE IS %@", self.productPrice);

NSString *obtainedProductPrice = self.productPrice;
NSLog(@"THE OBTAINED PRODUCT PRICE IS %@", obtainedProductPrice);

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:self.productBeingPurchased.localizedTitle message:self.productBeingPurchased.localizedDescription delegate:self.delegate cancelButtonTitle:obtainedProductPrice otherButtonTitles:@"Maybe later", nil];
alertView.tag = 999;
[alertView show];
}

- (NSString *)obtainPurchasePricing
{
// Displays Local Currency
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.formatterBehavior = NSNumberFormatterBehavior10_4;
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = self.productBeingPurchased.priceLocale;
self.productPrice = [NSString stringWithFormat:@"Buy this for %@", [formatter stringFromNumber:self.productBeingPurchased.price]];
NSLog(@"The price is %@", self.productPrice);
return self.productPrice;
}


All of the NSLogs are showing the price appropriately.


Now from my IAPViewController, I want the title of the UIButton to be the same as what it is set in the obtainPurchasePricing method, so:



NSString *obtainedPrice = [self.e100EntriesPurchase obtainPurchasePricing];
[self.iap100Button setTitle:obtainedPrice forState:UIControlStateNormal];
NSLog(@"The price is %@", obtainedPrice);


The problem I am facing is that the title of the UIButton is "Buy Now for (null)". The NSLog also doesn't show the actual price. This code above is in the viewDidLoad of the IAPViewController.


The properties in the e100EntriesPurchase class are:



@property (nonatomic, strong) SKProduct *productBeingPurchased;
@property (nonatomic, strong) NSString *productPrice;


What am I missing to obtain the actual price?


Any thoughts on this would really be appreciated!


Update


Through some extensive debugging, I have seen the pattern for what goes on when the UIButton is pressed (because at that point, the price is displayed correctly and therefore self.productsBeingPurchased is not nil).



- (void)validateProductIdentifiers
{
// Asking the App Store - I'm supposed to buy this product, does it exist? If it does, we'll get back a product.
NSLog(@"validateProductIdentifiers");
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:self.iap100EntriesPurchase]];
// Set the delegate
request.delegate = self;
// Call the start method on that request
[request start];

}

- (void)makeThePurchase
{
NSLog(@"makeThePurchase");
SKPayment *payment = [SKPayment paymentWithProduct:self.productBeingPurchased];
[[SKPaymentQueue defaultQueue]addPayment:payment];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"productsRequest");
// This method is saying there is a product request and is there a response reeived.
// We'll grab a reference to that product.

// There is only one product on iTunes Connect, so we can just grab that product. With more than one, we'd have to loop through all of the available products.
// grab a reference to our product.
self.productBeingPurchased = response.products.firstObject;

// Now that we have a product, let's display a store UI.

if ([SKPaymentQueue canMakePayments])
{
// Can we actually buy thigns? Yes we can buy stuff. It's a good idea to check this. We'll create a method to display the store UI.
[self displayStoreUI];
}
else
{
// in app purchaes are disabled in the settings
[self cantBuyAnything];
}
}


From the IAP, if I run [self.e100EntriesPurchase validateProductIdentifiers] before calling the obtainPurchasePricing, it still shows NULL as the button title (Buy Now for NULL), but it then invokes the displayStoreUI method after a few seconds, so that's clearly not right.


Possible Duplicate


The question has been marked as a possible duplicate, but none of the answers in the proposed question work for me; the issue is still the same even after I've set up the delegate mechanism.




Aucun commentaire:

Enregistrer un commentaire