无法在updatedTransactions委托中识别购买的产品

时间:2016-10-09 13:38:16

标签: ios objective-c in-app-purchase

我在IAP中有几个产品(在这个例子中有2个)我正在使用UITableView,用户将点击它并选择他们想要购买的产品。但是,我无法在updatedTransactions委托中识别用户已购买的交易。以下是我的代码:

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;
}

问题出在以下几行,我应该使用什么作为标识符,以便我可以根据用户的购买情况调用不同的功能?

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

1 个答案:

答案 0 :(得分:1)

您可以从SKPaymentTransaction对象获取产品标识符,例如transaction.payment.productIdentifier。只需使用以下代码替换if条件:

    if ([transaction.payment.productIdentifier isEqualToString:@"example.1coin"]){

        [self unlockFeature1coins];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"example.10coins"]){

        [self unlockFeature10coins];
    }