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

    - (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会累积。

我会犯错吗?
感谢您的帮助。

最佳答案

真的是我的错
每次购买时,我都会复制Store Observer对象
在添加防止重复的标志后(如下所示),该错误已修复。

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

关于objective-c - finishTransaction:不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5906204/

10-10 15:01