恢复应用内购买错误:即使未购买也会恢复

时间:2016-07-19 19:53:46

标签: ios in-app-purchase

一位(诚实)用户刚刚通知我,即使尚未购买应用内购买,只需按下RESTORE按钮即可“购买”应用内购买我的应用。 这是我正在使用的代码(我认为这已经足够......但我错了):

 - (IBAction) restore {

            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
            [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }


- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *) transactions {

    for (SKPaymentTransaction *transaction in transactions) {

        switch(transaction.transactionState){

            case SKPaymentTransactionStatePurchasing:

                NSLog(@"Transaction state -> Purchasing ...");

                break;

            case SKPaymentTransactionStatePurchased:

                NSLog(@"Transaction state -> Purchased");

                [self activatePurchase];

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                break;

            case SKPaymentTransactionStateRestored:

                NSLog(@"Transaction state -> Restored");

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];


                break;

            case SKPaymentTransactionStateFailed:

                NSLog(@"Transaction state -> Cancelled");

                if(transaction.error.code == SKErrorPaymentCancelled) {

                    //the user cancelled the payment ;(
                }

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                break;

            case SKPaymentTransactionStateDeferred:

                NSLog(@"Transaction state -> Deferred");

                break;
        }
    }
}




- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    [self activatePurchase];

}

有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

付款观察员方法paymentQueueRestoreCompletedTransactionsFinished在付款队列完成恢复交易时被调用,无论是否实际还原了任何交易。每次拨打paymentQueueRestoreCompletedTransactionsFinished时,即使拨打restoreCompletedTransactions有0次,您也会拨打updatedTransactions

因此,即使没有购买,您也会拨打activatePurchase

激活购买的正确位置位于SKPaymentTransactionStateRestored的{​​{1}}案例中;正如您对updateTransactions案例所做的那样。