finishTransaction:不起作用

时间:2011-05-06 02:32:29

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

我为应用内购买构建了一个项目。下面是部分源代码:

    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"paymentQueue update");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"completeTransaction");
    // Your application should implement these two methods.
    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"restoreTransaction");
    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"failedTransaction");
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Optionally, display an error here.
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

//Optional, record the transaction infomation
- (void) recordTransaction: (SKPaymentTransaction *)transaction{

    receipt=transaction.transactionReceipt;

    [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self sendReceipt:receipt];

    expiredTime=transaction.transactionDate;
}

我可以成功添加付款并在沙箱中购买,
但执行finishTransaction:后,已完成的交易不会从defaultQueue中删除 添加付款时,defaultQueue会累积。

我是否犯了一些错误?
感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这真的是我的错...... 我每次购买时都会复制StoreObserver对象 在我添加一个标志以防止重复(如下所示)之后,错误就被修复了。

static bool hasAddObserver=NO;
    -(void) recvPurchase:(NSString *)purchaseString{
        //check user's in-app purchase status
        if ([self CanPurchase]) {
                /*=====================================*/
            if (!hasAddObserver) {//flag to fix this bug
                /*=====================================*/
                observer=[[StoreObserver alloc]init];
                [[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
                hasAddObserver=YES;
            }
            SKPayment *payment = [SKPayment paymentWithProductIdentifier:purchaseString];
            [[SKPaymentQueue defaultQueue] addPayment:payment];
        }
    }