Calvin Calvin - 1 month ago 16
Objective-C Question

Unable to identify the purchased product in updatedTransactions delegates

I have a couple of products in IAP (in this example 2 of them) and I am using UITableView which the user will click and select which product they wanted to purchase. However I am not able to identify at the updatedTransactions delegate which transaction the user has purchased. Below are my codes:

NSArray *arrayProducts;

-(void) getProductInfo:(BLevelViewController *) viewController
{
if ([SKPaymentQueue canMakePayments])
{
_linkStatus.text = @"Able to purchase";
SKProductsRequest *requestProducts = [[SKProductsRequest alloc]
initWithProductIdentifiers:[NSSet setWithObjects:@"example.1coin", @"example.2coin", nil]];
requestProducts.delegate = self;
[requestProducts start];

}else{
_linkStatus.text = @"Please enable In App Purchase in Setting";
}
}

-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;

if (products.count != 0)
{
_linkStatus.text = @"Product loaded";
arrayProducts = products;

} else {
_linkStatus.text = @"Product no found";
arrayProducts = @[@"0",@"0"];
}

products = response.invalidProductIdentifiers;

for (SKProduct *product in products)
{
NSLog(@"Product not found: %@", product);
}

}


-(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0)
{
SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:0]];
[[SKPaymentQueue defaultQueue] addPayment:payment];

} else if (indexPath.row == 1)
{
SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:1]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){
[self unlockFeature1coin];
}
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){
[self unlockFeature10coins];
}
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;

case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;

case SKPaymentTransactionStatePurchasing:

break;


default:
break;
}
}
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

BCoinsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:coinsCellIdentifier];

if (indexPath.row == 0)
{
cell.cellLabel.text = @"Buy 10 coins";
[cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal];
}
else if (indexPath.row == 1)
{
cell.cellLabel.text = @"Buy 1 coin";
[cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal];
}

return cell;
}


The issue is at the lines below, what should I use as an identifier so that i can call different functions based on the user purchases?

if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){
[self unlockFeature1coins];
}
if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){
[self unlockFeature10coins];
}

Answer

You can get the product identifier from the SKPaymentTransaction object like this transaction.payment.productIdentifier. Just replace your if conditions with this code:

    if ([transaction.payment.productIdentifier isEqualToString:((SKProduct *)[arrayProducts objectAtIndex:0]).productIdentifier]){
        [self unlockFeature1coins];
    }
    else if ([transaction.payment.productIdentifier isEqualToString:((SKProduct *)[arrayProducts objectAtIndex:0]).productIdentifier]){
        [self unlockFeature10coins];
    }