SKproduct is nil for Buy Button

I followed a simple tutorial online and the In App functionality seems to be working for the most part. When my purchase app page loads it receives all my In App purchase items correctly but when I click the buy button my app crashes and my SKproduct is nil for that button. Has anyone ran into this issue before?

Answered by PBK in 132290022

The issue you have is that your _fvProduct is not retained in memory - and therefore it does not cause products[2] to be retained in memory. I create (alloc/init) an NSMutableArray in viewDidLoad. I then add the relevant product to that array and that causes the relevant product to be retained in memory within the mutable array. You can synthesize getters and then use a self.fvproduct approach. I am not familiar with the whole "_" and "self." stuff.

It is not possible to respond to your question because it lacks any specificity.


>....it receives all my In App purchase items

What method 'receives' them?

>....click the buy button

What code are you executing when you click some button?

>...my SKproduct is nil for that button.

What does this mean? A button does not have an SKproduct associated with it.

My array of products below are received during the productsRequest. My _fvProduct is assigned my 3rd In App purchase item.

My products below are of SKProduct.

#pragma mark -

#pragma mark SKProductsRequestDelegate

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

{


NSArray *products = response.products;


if (products.count != 0)

{

_fvProduct = products[2];

fvBuyButton.enabled = YES;

fvProductTitle.text = _fvProduct.localizedTitle;

_ddProduct = products[1];

ddBuyButton.enabled = YES;

ddProductTitle.text = _ddProduct.localizedTitle;

_bundleProduct = products[0];

bundleBuyButton.enabled = YES;

bundleProductTitle.text = _bundleProduct.localizedTitle;

} else {

fvProductTitle.text = @"Full Version Product not found";

ddProductTitle.text = @"Dirty Dozen Product not found";

bundleProductTitle.text = @"Bundle Product not found";

}


products = response.invalidProductIdentifiers;


for (SKProduct *product in products)

{

NSLog(@"Product not found: %@", product);

}

}


Once I call the buyFVProduct, the _fvProduct is nil. Once the button action is finished that is when the game will crash. I'm not sure if the SKPaymentQueue cannot be called due to the _fvProduct being nil or I'm calling the SKPaymentQueue incorrectly.


- (IBAction)buyFVProduct:(id)sender {

SKPayment *payment = [SKPayment paymentWithProduct:_fvProduct];

[[SKPaymentQueue defaultQueue] addPayment:payment];

}


Sorry about the lack of information before. I appreciate any help with this.

Accepted Answer

The issue you have is that your _fvProduct is not retained in memory - and therefore it does not cause products[2] to be retained in memory. I create (alloc/init) an NSMutableArray in viewDidLoad. I then add the relevant product to that array and that causes the relevant product to be retained in memory within the mutable array. You can synthesize getters and then use a self.fvproduct approach. I am not familiar with the whole "_" and "self." stuff.

Wow I don't even remember posting this question. I should've responded once I found the solution, my apologies. I no longer have the code and rewrote the entire app in swift last year.

You are most likely correct that the data for the products was not being retained. My explanation isn't clear as this could be an issue with the app store data. It is possible that only the first 2 products were received in the response therefore no data could have ever been used for that button action.


SKproduct is nil for Buy Button
 
 
Q